Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing - check status of each processes

I wonder if it is possible to check how long of each processes take.
for example, there are four workers and the job should take no more than 10 seconds, but one of worker take more than 10 seconds.Is there way to raise a alert after 10 seconds and before process finish the job.
My initial thought is using manager, but it seems I have wait till process finished.
Many thanks.

like image 665
galaxyan Avatar asked Sep 29 '16 14:09

galaxyan


1 Answers

You can check whether process is alive after you tried to join it. Don't forget to set timeout otherwise it'll wait until job is finished.

Here is simple example for you

from multiprocessing import Process
import time

def task():
    import time
    time.sleep(5)

procs = []

for x in range(2):
    proc = Process(target=task)
    procs.append(proc)
    proc.start()

time.sleep(2)

for proc in procs:
    proc.join(timeout=0)
    if proc.is_alive():
        print "Job is not finished!"
like image 73
Darth Kotik Avatar answered Sep 18 '22 19:09

Darth Kotik