2

I am trying to run two subprocesses simultaneously. My current program looks like if one of subprocess has error and terminated, it cannot relaunch again after 1 min. It needs to wait another subprocess failed also and both of them start together. I found some posts about how to kill specific subprogress, but how can I (1) if one subprocess has error, all subprocesses will be terminated and relaunch again after 1 min? Or (2) the subprocess with error can relaunch again after 1 min without waiting another running subprocess? Will it work if I change

proc.wait()

to

proc.kill()

in the following code? Is there any better way to deal with it?

import sys
import subprocess
import os               
import time

def executeSomething():
    # This setting is very important to avoid errors 
    os.environ['PYTHONIOENCODING'] = 'utf-8'    

    procs = []
    files = ["TwitterDownloader_1.py","TwitterDownloader_2.py"]   
    try:
        for i in files:
            proc = subprocess.Popen([sys.executable,i])
            procs.append(proc)

        for proc in procs:
            proc.wait() 
    except AttributeError:
        print 'Attribute Error found and skiped' 
    time.sleep(61)

while True:
    executeSomething()       
michaelpri
  • 3,521
  • 4
  • 30
  • 46
Wei-Ting Liao
  • 115
  • 2
  • 9
  • possible duplicate of [How to make child process die after parent exits?](https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits/23401172#23401172) – Greg Hewgill May 12 '15 at 20:29
  • It seems like I need to define parents and child programs and let child program send signal to tell parents stop all subprocess? – Wei-Ting Liao May 12 '15 at 21:20

1 Answers1

1

How about running a loop to check the status of the processes?

Something like this:

for proc in procs:
    if proc.poll() is not None:  # it has terminated
        # check returncode and handle success / failure
Barry Rogerson
  • 598
  • 2
  • 15