'How can I speed up the routing process in OSMNX?

Say I have two taxi orders with Origin1、Destination1 and Origin2、Destination2(O1,O2,D1,D2). I want to calculate the possibility of ridesharing, so I need the path between two different points. And, here's my code:

def path_time(point1, point2):
    path = ox.distance.shortest_path(road, point1, point2, weight='travel_time')
    if path is None:
        #If there isn't a path, a big weight will be set, and it won't be selected during the matching process.
        route_time = 9999
    else:
        route_time = int(sum(ox.utils_graph.get_route_edge_attributes(road, path, "travel_time")))
    return route_time,path

Since there is four points, I need to do this six times, where tp means travel path :

tpO1O2 = path_time(O1,O2)
tpO1D1 = path_time(O1,D1)
tpO1D2 = path_time(O1,D2)
tpO2D1 = path_time(O2,D1)
tpO2D2 = path_time(O2,D2)
tpD1D2 = path_time(D1,D2)

It's okay if I only have two points, but I got a 2 million order set, and each order has hundreds of potential matched orders. So this will take me a lot of time.

Does anyone knows how can I speed up this process? Thank you!



Solution 1:[1]

You can use parallelization to speed this up. Parallel route solving is built into OSMnx. Per the documentation:

You can parallelize solving multiple paths with the cpus parameter

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gboeing