如何在有序关闭子进程的同时随时杀死子进程 [英] How to kill sub process in anytime while closing it in orderly fashion

查看:34
本文介绍了如何在有序关闭子进程的同时随时杀死子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行多个测试文件的 GUI.所有的测试都是python文件,GUI使用从线程运行的子进程一一运行.

I have a GUI that run multiple tests files. All the tests are python files that the GUI run one by one using subprocess running from thread.

在 GUI 中,用户可以停止运行测试的机器.我已经找到了如何为此目的终止子进程及其工作原理.请参阅杀死在线程内运行的子进程

In the GUI , the user has the ability to stop the machine that run the tests. I already found out how to kill a subprocess for this purpose and its works. see Killing sub process that run inside a thread

现在,我还有一个问题.测试打开了许多我必须按顺序关闭的实例.当我使用 kill() 方法时,所有实例都没有关闭,我无法运行进一步的测试.I.E 在用户停止之前我在测试中使用的 COM 端口仍然被占用并阻止我运行更多测试.

Now , I have another issue. The test open lots of instances that I must close in orderly fashion. When I use kill() method , all the instances are not closed and I cant run further tests. I.E the COM port I used in the test before the stop from the user , is still occupied and prevent me to run any more tests.

唯一的补救方法是关闭 GUI 并重新开始.

The only remedy for it, will be closing the GUI and start it over.

简而言之,我需要一种随时终止子进程的方法,但仍然关闭所有实例,例如 sys.exit() 会做或关闭 GUI 具有的相同效果.我尝试使用 wait(timeout) 但除非我使用错误,否则它不会起作用.

In short ,I need a way to kill a subprocess in anytime but still close all the instances like sys.exit() will do or the same effect that closing he GUI has. I've try to use wait(timeout) but unless I'm using it wrongly , it don't do the trick.

我可以对测试使用某种中断来调用测试中的某些方法,从而以有序的方式关闭它吗?

Can I use kind of interrupt to the test that will call some method in the test , that will close it in orderly fashion?

推荐答案

你应该能够使用类似这个解决方案 识别和终止应用程序启动的子进程来运行测试.此解决方案似乎特别适合您杀死所有后台进程以允许您的应用程序运行更多测试的用例.

You should be able to use something like this solution to identify and terminate the subprocesses launched by the application to run tests. This solution seems to particularly fit your use case of killing all background processes in order to allow your application to run more tests.

或者,您可以为您的测试创建一个 try-except 包装器或装饰器,允许在应用程序发送特定信号时将它们杀死,但这需要应用程序跟踪所有启动的子进程,如果测试在后台启动其他子进程,则可能无法正确终止所有进程.

Alternatively, you may be able to create a try-except wrapper or decorator for your tests that allow them to be killed when sent a certain signal is sent by the application, but this requires the application to keep track of all initiated subprocesses and may fail to properly kill all processes if the tests launch additional subprocesses in the background.

这篇关于如何在有序关闭子进程的同时随时杀死子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆