Where to call join() when multiprocessing

后端 未结 1 1572
[愿得一人]
[愿得一人] 2021-01-25 06:00

When using multiprocessing in Python, I usually see examples where the join() function is called in a separate loop to where each process was actually created.

相关标签:
1条回答
  • 2021-01-25 06:13

    join() is blocking operation.

    In first example you start 10 processes and then you are waiting for all procces to finish. All processes are running at same time.

    In second example you start one process at time and you are waiting for finish before you start another process. There is only one running process at same time

    First example:

    def wait()
        time.sleep(1)
    
    # You start 10 processes
    for i in range(10):
        p = Process(target=wait)
        processes.append(p)
        p.start()
    
    # One second after all processes can be finished you check them all and finish
    for p in processes:
        p.join()
    

    Execution time of whole script can be near one second.

    Second example:

    for i in range(10):
        p = Process(target=wait) # Here you start one process 
        processes.append(p)
        p.start()
        p.join() # Here you will have to wait one second before process finished.
    

    Execution time of whole script can be near 10 seconds!.

    0 讨论(0)
提交回复
热议问题