Mutiprocessing/parallel programming

from multiprocessing import Process
def square(x=2):
    for i in range(10000000):
        pass
    print(x**2)
procs=[]
for i in range(5):
    procs.append(Process(target=square))
for proc in procs:
    proc.start()
for proc in procs:
    proc.join()

what is the use of

for proc in procs:
    proc.join()

in the above code? it gives the same output with and without that line

Hey @devchopra999_11c6416ab7f09bbf,
Of course it will work same but basically join method blocks the execution of the main process until the process whose join method is called terminates. Without the join method, the main process won’t wait until the process gets terminated. So we use join to wait for all the processes to complete first before resuming main process.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

whats the main process? everything excluding the parallel processes we created?

Yes all the task created will be running parallel in the main process or main thread.

1 Like