Skip to content

Commit

Permalink
protect entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Feb 18, 2025
1 parent ce67744 commit 1c076b4
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions notebooks/02-routing-speed-time.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@
"source": [
"## 4. Multiprocessing\n",
"\n",
"Calculating lots of shortest paths can be slow, but OSMnx has built-in shortest path solver parallelization and multiprocessing. With the `shortest_path` function, you can pass in a single origin-destination pair to solve the one shortest path, or you can pass in lists of origins and destinations to solve each shortest path between the pairs. If you're solving shortest paths for multiple origins/destinations, the `cpus` argument determines how many CPU cores to utilize for parallelized solving. Multiprocessing adds some overhead, so it's only faster if you're solving a lot of paths. It also has substantial RAM requirements (as it must copy the graph into each sub-process), so be careful with your RAM when setting the `cpus` argument."
"Calculating lots of shortest paths can be slow, but OSMnx has built-in shortest path solver parallelization and multiprocessing. With the `shortest_path` function, you can pass in a single origin-destination pair to solve the one shortest path, or you can pass in lists of origins and destinations to solve each shortest path between the pairs.\n",
"\n",
"If you're solving shortest paths for multiple origins/destinations, the `cpus` argument determines how many CPU cores to utilize for parallelized solving. Multiprocessing adds some overhead, but it's faster if you're solving a lot of paths. It also has higher RAM requirements (as it must copy data into each sub-process), so be careful with your RAM when setting the `cpus` argument.\n",
"\n",
"If you are multiprocessing, always remember to protect your entry point. From the [Python docs](https://docs.python.org/3/library/multiprocessing.html#multiprocessing-programming):\n",
"\n",
"> Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such as starting a new process)... one should protect the “entry point” of the program by using `if __name__ == '__main__':`"
]
},
{
Expand All @@ -273,20 +279,13 @@
"metadata": {},
"outputs": [],
"source": [
"# how many CPUs do you have\n",
"mp.cpu_count()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %%time\n",
"# it takes 2.3 seconds to solve all the routes using all the cores on my computer\n",
"# I have a 24-thread AMD 5900x: performance will depend on your specific CPU\n",
"# routes = ox.routing.shortest_path(G, origs, dests, weight=\"travel_time\", cpus=None)"
"if __name__ == \"__main__\":\n",
" # how many CPU cores do you have\n",
" print(mp.cpu_count())\n",
" routes = ox.routing.shortest_path(G, origs, dests, weight=\"travel_time\", cpus=None)\n",
" print(len(routes))"
]
},
{
Expand Down

0 comments on commit 1c076b4

Please sign in to comment.