최근에 일부 병렬 프로세스를 풀로 리팩터링하여 풀이 순수 프로세스의 거의 두 배가 걸린 것에 놀랐습니다. 동일한 수의 코어로 동일한 시스템에서 실행되고 있다고 가정하십시오.풀이 동일한 프로세스 수보다 느리다 이유
공유 의존성 :
여기https://github.com/taynaud/python-louvain
from community import best_partition
이 Process
를 사용하여 빠른 구현 누군가가 풀을 사용하여 내 구현이 오래 걸리는 이유를 설명 아마도 몇 가지 조언을 제공 할 수 있기를 바랍니다. [UPDATE]는 여전히 신속 풀 구현 동일 액티브 프로세스의 수를 제어하기 위해 리팩토링 여기
processes = []
pipes = []
def _get_partition(send_end):
send_end.send(best_partition(a_graph, resolution=res, randomize=rand))
for idx in range(iterations):
recv_end, send_end = Pipe(False)
p = Process(target=_get_partition, args=(send_end,))
processes.append(p)
pipes.append(recv_end)
running_procs = []
finished_procs = []
while len(finished_procs) < iterations:
while len(running_procs) < n_cores and len(processes):
proc = processes.pop()
proc.start()
running_procs.append(proc)
for idx, proc in enumerate(running_procs):
if not proc.is_alive():
finished_procs.append(running_procs.pop(idx))
for p in finished_procs:
p.join()
partitions = [pipe.recv() for pipe in pipes]
및 느린, Pool
구현이다.
pool = Pool(processes=n_cores)
results = [
pool.apply_async(
best_partition,
(a_graph,),
dict(resolution=res, randomize=rand)
) for i in range(iterations)
]
partitions = [res.get() for res in results]
pool.close()
pool.join()
첫 번째 예에서는 잠재적으로 더 많은 프로세스를 생성하고 있습니다. – Shadow
Shadow에 답해 주셔서 감사합니다. 저는 같은 시스템에서 실행 중이었고 풀은 장비가 제공해야하는 최대 코어 수를 사용하고있었습니다. – jfunk
시간 측정에는 정확히 무엇이 포함되어 있습니까? 두 경우 모두 또는 전체 코드 스 니펫? – roganjosh