Оптимизирующая функция с использованием multiprocessing.Pool()Python

Программы на Python
Anonymous
Оптимизирующая функция с использованием multiprocessing.Pool()

Сообщение Anonymous »

Я новичок в теме многопроцессорности. Я использую мощную машину с большим количеством ядер и хотел бы задействовать все доступные ядра.
Это оригинальная функция, которая решает задачу очень медленно:
def get_multimodal_poi_directness(to_bus_stop_graph, bus_stop_graph, from_bus_stop_graph, start_node, target_nodes, start_time,
weight_threshold, mode):

reachable_nodes = set()

bus_node, path_length = get_bus_station_from_isochrone(start_node)

end_time = start_time + timedelta(minutes=weight_threshold)

if path_length > weight_threshold:
return set()

# Step 3: Find all reachable nodes from the nearest bus stop node within the remaining weight
current_time = start_time + timedelta(minutes=path_length)
remaining_time = weight_threshold - path_length

reachable_nodes_dict = reachable_nodes_via_bus_network(bus_stop_graph, bus_node, remaining_time, current_time, end_time, mode=mode)
reachable_nodes.update(reachable_nodes_dict.keys())
# Step 4: Find all reachable nodes from the bus stop nodes to POIs
all_reachable_nodes = reachable_nodes_to_pois(from_bus_stop_graph, reachable_nodes_dict, end_time)

reachable_nodes.update(all_reachable_nodes)
# Step 5: Calculate the number of POIs reachable from the start node
# node_attributes = nx.get_node_attributes(from_bus_stop_graph, 'poi')
# poi_count = sum(node_attributes.get(node, 0) for node in all_reachable_nodes)

return reachable_nodes


Это модифицированная функция, использующая multiprocessing.Pool()

def worker(args):
return reachable_nodes_via_bus_network(\*args)

def get_multimodal_poi_directness_cpu(to_bus_stop_graph, bus_stop_graph, from_bus_stop_graph, start_node, target_nodes, start_time,
weight_threshold, mode):

reachable_nodes = set()

bus_node, path_length = get_bus_station_from_isochrone(start_node)

end_time = start_time + timedelta(minutes=weight_threshold)

if path_length > weight_threshold:
return set()

# Step 3: Find all reachable nodes from the nearest bus stop node within the remaining weight
current_time = start_time + timedelta(minutes=path_length)
remaining_time = weight_threshold - path_length

args = (bus_stop_graph, bus_node, remaining_time, current_time, end_time, mode)

with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
reachable_nodes_dict = pool.map(worker, [args])[0]

reachable_nodes.update(reachable_nodes_dict.keys())

# Step 4: Find all reachable nodes from the bus stop nodes to POIs
all_reachable_nodes = reachable_nodes_to_pois(from_bus_stop_graph, reachable_nodes_dict, end_time)

reachable_nodes.update(all_reachable_nodes)

return reachable_nodes

Я протестировал новую функцию, и она работает медленнее, чем раньше. Основная цель – повысить эффективность работы достижимых_узлов_через_bus_network.


Подробнее здесь: https://stackoverflow.com/questions/789 ... ssing-pool

Вернуться в «Python»