4

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....

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

4 Answers4

5

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.

Thomas
  • 174,939
  • 50
  • 355
  • 478
4

p.is_alive() tells you whether the process is running.

To wait until it's ended, use p.join().

grovina
  • 2,999
  • 19
  • 25
1

you can use the join method (read more in docs):

First, keep the Process objects you create:

your_processes = []
...
   p.start()
   your_processes.append(p)

After starting all your processes, use join to wait each one of them to finish executing.:

for p in your_processes:
    p.join()
... #the rest of your code

Basically join makes the main process wait for process p to finish and only then to proceed to the next line of code.

AndreyF
  • 1,798
  • 1
  • 14
  • 25
1

You can do like this:

P = []
for i in range(1, processes + 1):
            hup = MyURLParser() //A class I made
            p = Process(target = hup.run)
            p.start()
            P.append(p)

for p in P:
            p.join()

To only check if the process is alive, use .is_alive()

Read more here: https://docs.python.org/3/library/multiprocessing.html

klutt
  • 30,332
  • 17
  • 55
  • 95