I am starting a process by doing:
for i in range(1, processes + 1):
hup = MyURLParser() //A class I made
p = Process(target = hup.run)
p.start()
After that, so that the main thread doesn't exit immediately I do:
while (True):
print("Main sleeping...")
sleep(20)
Instead of doing this, how can I check that each of the child processes are still running from the main thread ? Then instead of having the infinite loop I can break out of the loop at the right time and do something else....
Add all processes to a list, and join()
each of them in turn:
processes = []
for i in range(1, processes + 1):
hup = MyURLParser() //A class I made
p = Process(target = hup.run)
p.start()
processes.append(p)
for p in processes:
p.join()
The join()
call blocks until the process is finished. It doesn't matter in which order you call them; calling join()
on a process that's already complete will just return immediately.
You may also want to check out the other functionality in the multiprocessing
module; in particular the Pool
class might help you simplify this code.
p.is_alive()
tells you whether the process is running.
To wait until it's ended, use p.join()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With