Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for all python processes to finish

I'm starting several python processes with downloads in a loop that calls this piece of code:

startTimeMillis = int(round(time.time() * 1000))

for i in range(10):
        p = multiprocessing.Process(target=performCurl, args =("http://www.google.com/d", i, ))
        p.start()


endTimeMillis = int(round(time.time() * 1000))
totalTimeSeconds = (endTimeMillis - startTimeMillis)
print "The whole process took ", str(totalTimeSeconds)

I want to check the time it takes for all the processes to finish, so how would I make the last part of the code to wait for all the processes?

like image 733
Kreender Avatar asked May 11 '26 09:05

Kreender


1 Answers

Use p.join() to wait for a process to terminate

all_processes = [multiprocessing.Process(...) for ... in ...]
for p in all_processes:
  p.start()

for p in all_processes:
  p.join()
like image 94
Thomas Avatar answered May 12 '26 23:05

Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!