简体   繁体   中英

Python: Run 2 processes in parallel then wait for all with timeout

Looking for some best practices for the following python3 task:

  • run process program1 in non-blocking mode
  • run process program2 in non-blocking mode
  • wait for all or kill them after timeout exceeded

I reckon I know how to do it for 1 process:

import subprocess
p = subprocess.Popen('program1', shell=True)
try:
    stdout, stderr = p.communicate(timeout=200)
except subprocess.TimeoutExpired as t:
    print(t)
    p.kill()
    outs, errs = p.communicate()

However I can't expand this approach for 2-processes case, because p.communicate blocks until program1 ends or timeout exceeds.

Also I'd like to know immediately if one of the programs fails.

Python3, OS Linux

UPD I need to implement it wisely, without any busy loops, threads etc

Process has a join() method similar to threads. So, in your case:

proc_1 = Process(target=my_func_1)
proc_2 = Process(target=my_func_2)

for proc in [proc_1, proc_2]:
    proc.start()

# They are doing their thing and when they've completed they will join the 
# main process and your main program can resume
for proc in [proc_1, proc_2]:
    proc.join()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM