Abh*_*kur 6 python subprocess nohup
我正在使用subprocess.Popen启动多个进程.
代码是这样的:
while flag > 0:
flag = check_flag()
c = MyClass(num_process=10)
c.launch()
Run Code Online (Sandbox Code Playgroud)
MyClass 如果类似以下内容:
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)
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,启动的子流程有时会在运行过程中死亡.在某些情况下,它完成.
但是,如果我subprocess.Popen在while循环中直接使用,则该过程会继续并及时完成.
有人能告诉我如何通过上面描述的方式使用子进程来让进程在后台运行?
在nohup当你的主进程正常退出只停止SIGHUP信号。对于 SIGINT 或 SIGTERM 等其他信号,子进程接收到与父进程相同的信号,因为它在同一个进程组中。有两种使用 Popenpreexec_fn参数的方法。
subprocess.Popen(['nohup', 'python', '/home/mypythonfile.py'],
stdout=open('/dev/null', 'w'),
stderr=open('logfile.log', 'a'),
preexec_fn=os.setpgrp )
Run Code Online (Sandbox Code Playgroud)
更多信息在另一篇文章中。
def preexec_function():
signal.signal(signal.SIGINT, signal.SIG_IGN)
subprocess.Popen( ... , preexec_fn=preexec_function)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2885 次 |
| 最近记录: |