OSX活动监视器中终止挂起时的python子进程(包含子进程的进程)
我正在为我编写的udp服务器编写一个测试工具。其思想是使用multiprocessing.pool为服务器()的多个实例执行subprocess.call命令。udp服务器是另一个python程序,由3个进程(主进程、http接口和udp服务器)组成。在我尝试编写测试脚本时,当我用CTRL+C终止测试脚本时,我的活动监视器中总是有2-5个挂起的python2.7进程 我的尝试:OSX活动监视器中终止挂起时的python子进程(包含子进程的进程),python,macos,subprocess,multiprocessing,Python,Macos,Subprocess,Multiprocessing,我正在为我编写的udp服务器编写一个测试工具。其思想是使用multiprocessing.pool为服务器()的多个实例执行subprocess.call命令。udp服务器是另一个python程序,由3个进程(主进程、http接口和udp服务器)组成。在我尝试编写测试脚本时,当我用CTRL+C终止测试脚本时,我的活动监视器中总是有2-5个挂起的python2.7进程 我的尝试: 香草子流程 处理未连接到终端()的bash的子进程 multiprocess.pool,还使用simple test.
1) 仅使用子流程:
args0 = [sys.executable, server_path, '-p', '7000', '-t', '8000', '-u', '9000']
args1 = [sys.executable, server_path, '-p', '7001', '-t', '8001', '-u', '9001']
p0 = subprocess.Popen(args0)
p1 = subprocess.Popen(args1)
2) 将子流程与stdout、pipe、os.kill一起使用:
p0 = subprocess.Popen( args0,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
close_fds=True)
print "running for 5s..."
time.sleep(5)
os.kill(p0.pid, signal.SIGTERM)
3a)测试_.py(带池)
3b)测试点
终于成功了。此代码生成两个多线程服务器实例,作为测试线束中的子进程。您将从子流程获得所有控制台输出,当您从终端按住CTRL+C组合键控制测试线束时,所有子流程也将死亡
subprocess.Popen
最终对我不起作用
def runInstance(args):
# NOTE: We don't do the stdout or stderr args
p = subprocess.call(args,
# stdout=subprocess.PIPE,
# stderr=subprocess.STDOUT,
close_fds=True,
shell=True
)
def main():
print "Starting test harness..."
server_path = os.getcwd() + '/' + 'server.py'
args0 = [sys.executable, server_path, '-p', '7000', '-t', '8000', '-u', '9000']
args1 = [sys.executable, server_path, '-p', '7001', '-t', '8001', '-u', '9001']
# Start server instances
# NOTE: It is target=runInstance, not target=runInstance()
p0 = multiprocessing.Process(target=runInstance, args=(' '.join(args0),))
p1 = multiprocessing.Process(target=runInstance, args=(' '.join(args1),))
p0.start()
p1.start()
好的,如果os.kill(p0.pid,signal.SIGTERM)
没有杀死子进程,那么听起来您的子进程正在忽略SIGTERM
。
def main():
while True:
a = 1
if __name__ == '__main__':
main()
def runInstance(args):
# NOTE: We don't do the stdout or stderr args
p = subprocess.call(args,
# stdout=subprocess.PIPE,
# stderr=subprocess.STDOUT,
close_fds=True,
shell=True
)
def main():
print "Starting test harness..."
server_path = os.getcwd() + '/' + 'server.py'
args0 = [sys.executable, server_path, '-p', '7000', '-t', '8000', '-u', '9000']
args1 = [sys.executable, server_path, '-p', '7001', '-t', '8001', '-u', '9001']
# Start server instances
# NOTE: It is target=runInstance, not target=runInstance()
p0 = multiprocessing.Process(target=runInstance, args=(' '.join(args0),))
p1 = multiprocessing.Process(target=runInstance, args=(' '.join(args1),))
p0.start()
p1.start()