有没有办法确保所有创建的子进程在Python程序的退出时间都死了?通过子进程,我指的是使用subprocess.Popen()创建的.
如果没有,我应该迭代所有发出的杀戮然后杀死-9?什么更干净?
我正在使用python 2.5上的子进程模块生成一个java程序(准确地说是selenium服务器),如下所示:
import os
import subprocess
display = 0
log_file_path = "/tmp/selenium_log.txt"
selenium_port = 4455
selenium_folder_path = "/wherever/selenium/lies"
env = os.environ
env["DISPLAY"] = ":%d.0" % display
command = ["java",
"-server",
"-jar",
'selenium-server.jar',
"-port %d" % selenium_port]
log = open(log_file_path, 'a')
comm = ' '.join(command)
selenium_server_process = subprocess.Popen(comm,
cwd=selenium_folder_path,
stdout=log,
stderr=log,
env=env,
shell=True)
Run Code Online (Sandbox Code Playgroud)
一旦自动化测试完成,该过程应该被杀死.我正在使用os.kill这个:
os.killpg(selenium_server_process.pid, signal.SIGTERM)
selenium_server_process.wait()
Run Code Online (Sandbox Code Playgroud)
这不起作用.原因是shell子进程为java生成了另一个进程,并且我的python代码不知道该进程的pid.我已经尝试过杀死进程组os.killpg,但是这也会杀死运行此代码的python进程.由于其他原因,将shell设置为false,从而避免java在shell环境中运行也是不可能的.
如何杀死shell以及由它生成的任何其他进程?