What exactly is Python multiprocessing Module's .join() Method Doing?

前端 未结 6 896
梦如初夏
梦如初夏 2020-11-29 16:09

Learning about Python Multiprocessing (from a PMOTW article) and would love some clarification on what exactly the join() method is doing.

In an old tut

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 16:48

    The join() call ensures that subsequent lines of your code are not called before all the multiprocessing processes are completed.

    For example, without the join(), the following code will call restart_program() even before the processes finish, which is similar to asynchronous and is not what we want (you can try):

    num_processes = 5
    
    for i in range(num_processes):
        p = multiprocessing.Process(target=calculate_stuff, args=(i,))
        p.start()
        processes.append(p)
    for p in processes:
        p.join() # call to ensure subsequent line (e.g. restart_program) 
                 # is not called until all processes finish
    
    restart_program()
    

提交回复
热议问题