如何立即停止正在运行单元测试的 python 子进程?终止并杀死不起作用
我有一个 Tkinter GUI 运行两个线程,GUI 的主要线程和工作线程。工作线程使用以下代码创建一个子进程:
myProcess = subprocess.Popen(['python', '-u', 'runTests.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
文件 runTests.py 进行一些设置,然后使用以下命令运行单元测试文件:
execfile('myUnitTests.py')
文件 myUnitTests.py 有几个单元测试,其中一些需要五分钟以上的时间才能运行。 在 GUI 中,我单击一个按钮来停止运行测试。这反过来又使工作线程发送信号来停止子进程:
myProcess.terminate()
终止命令不会立即停止进程,而是等到当前单元测试运行完毕然后终止进程? 我尝试过使用 os.kill,但得到与 Terminate() 相同的结果。
知道如何使我的程序更具响应性,以便它立即终止子进程吗?
I have a Tkinter GUI running two threads, the main tread for the GUI and a worker thread. The worker thread creates a subprocess using the following code:
myProcess = subprocess.Popen(['python', '-u', 'runTests.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
The file runTests.py does some setup and then runs a unit test file using the following command:
execfile('myUnitTests.py')
The file myUnitTests.py has several unit tests some that take over five minutes to run.
From the GUI I click a button to stop running the tests. This in turn makes the worker thread send a signal to stop the subprocess:
myProcess.terminate()
The terminate command does not stop the process right away, it waits until the current unit test finishes running and then it terminates the process?
I have tried to use os.kill
but I get the same results as with terminate()
.
Any idea of how can I make my program more responsive so that it kill the subprocess right away?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python 文档 [ http://docs.python.org/library/signal.html ] 说:
因此,如果您的五分钟单元测试正在执行“纯粹用 C 实现的长计算”,并且您的单元测试工具安装了
SIGTERM
的处理程序,那么这就是您的问题。如果是这样,请尝试使用myProcess.kill
而不是myProcess.terminate
(或者,如果您还没有 2.6,则使用myProcess.send_signal(9)
)。SIGKILL
无法从用户空间捕获,并且应该立即生效。警告:任何应该在单元测试框架之外运行的清理操作都不会被执行。
The Python documentation [ http://docs.python.org/library/signal.html ] says:
So if your five-minute unit test is doing "a long calculation implemented purely in C", and your unit test harness installs a handler for
SIGTERM
, that's your problem. If so, trymyProcess.kill
instead ofmyProcess.terminate
(or, if you haven't got 2.6,myProcess.send_signal(9)
).SIGKILL
is uncatchable from user space and should have immediate effect.Warning: any clean-up actions that are supposed to run on the way out of your unit test framework will not be executed.