I'm using subprocess.Popen
to launch several processes.
The code is something like this:
while flag > 0:
flag = check_flag()
c = MyClass(num_process=10)
c.launch()
MyClass
if something like the following:
MyClass(object)
def __init__(self, num_process):
self.num_process = num_process
def launch(self):
if self.check_something() < 10:
for i in range(self.num_process):
self.launch_subprocess()
def launch_subprocess(self):
subprocess.Popen(["nohup",
"python",
"/home/mypythonfile.py"],
stdout=open('/dev/null', 'w'),
stderr=open('logfile.log', 'w'),
shell=False)
In most of the cases, the launched subprocess dies, sometimes in the middle of the run. In some cases, it completes.
However, if I use subprocess.Popen
directly in the while loop, the process continues and finished timely.
Could someone tell me how can I get the processes to run in the background by using subprocess in the way as I described above?
The nohup
only stop the SIGHUP signal when your master process exits normally. For other signal like SIGINT or SIGTERM, the child process receives the same signal as your parent process because it's in the same process group. There're two methods using the Popen's preexec_fn
argument.
subprocess.Popen(['nohup', 'python', '/home/mypythonfile.py'],
stdout=open('/dev/null', 'w'),
stderr=open('logfile.log', 'a'),
preexec_fn=os.setpgrp )
More information goes in another post.
def preexec_function():
signal.signal(signal.SIGINT, signal.SIG_IGN)
subprocess.Popen( ... , preexec_fn=preexec_function)
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