我正在使用
代码如下:
subprocess.Popen
启动多个进程。代码如下:
while flag > 0:
flag = check_flag()
c = MyClass(num_process=10)
c.launch()
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)
在大多数情况下,启动的子进程会死亡,有时是在运行中途。 在某些情况下,它会完成。
但是,如果我直接在while循环中使用subprocess.Popen
,该进程将继续并及时完成。
有人能告诉我如何通过上述方式使用subprocess使进程在后台运行吗?
multiprocessing
模块可以帮助您并行运行该脚本的代码。 - bbaylesPopen()
单独启动进程“在后台”(https://dev59.com/M1oU5IYBdhLWcg3w1pX2#37059478) 2- “子进程被杀死”是什么意思?你怎么知道?ps
输出?你在bash中按下Ctrl+C
吗?你退出shell(Ctrl+D
)吗?你看到任何错误吗?你尝试生成唯一的日志文件名或使用追加模式:open('logfile.log', 'ab', 0)
? 3- 无关:使用close_fds=True
。 - jfs