我有ac程序基本上是在无限While循环中运行,如下所示.
int main(int argc, char** argv )
{
Detectdirection *d;
//Mosquitto
io_service io;
deadline_timer t(io);
MosquitoInterface *m = new MosquitoInterface(t);
d = new Detectdirection();
while(true)
{
int ret = d->Tracking();
if(ret < 0)
cout << "Pattern is not found" << endl ;
}
delete d;
delete m;
cout << "Exit" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我喜欢从python脚本运行和停止程序.运行程序非常简单.所讨论的只是提供一个路径构建文件在这里.
但是如何从Pyhton中停止应用程序,以便正确删除创建的对象.
以下(一个小的C程序和一个调用它的python脚本)在各种Unix上表现不同.
在其中一些(例如Debian stable)中,C app获取信号,消息从信号处理程序打印得很好并且脚本完成.在其他人(例如,两年前的Ubuntu和OpenBSD)中,信号丢失,因此根本不打印消息,并且脚本永远等待...
信号总是传递,如果在python脚本中,我改变了这个......
mysub=subprocess.Popen("./cryInAbort", shell=True)
Run Code Online (Sandbox Code Playgroud)
进入这...
mysub=subprocess.Popen("./cryInAbort", shell=False)
Run Code Online (Sandbox Code Playgroud)
所以看起来在某些Unix中,中间shell"吃掉"SIGINT,而在其他Unix中,它将它转发给子进程(C程序).
我注意只在信号处理程序中调用重入函数,所以这似乎与我的C代码无关 - 它看起来好像是在"/ bin/sh"中的信号处理行为(Python使用的默认shell)产生的东西)在Unix上并不"稳定"......
难道我做错了什么?
编辑:如果你想知道为什么我使用"shell = True":这是因为在我的真实代码中,我不只是传递"./executable" - 我使用shell代码进行循环和通配符.
这是在获得SIGINT时打印和死亡的C代码:
#include <signal.h>
#include <unistd.h>
void my_handler()
{
static const char msg[] = "Goodbye - test was OK.\n";
write(1,msg,sizeof(msg));
fsync(1);
_exit (0);
}
int main()
{
(void) signal (SIGINT, my_handler);
while (1);
}
Run Code Online (Sandbox Code Playgroud)
这是通过发送SIGINT来测试它的Python脚本:
#!/usr/bin/env python
import subprocess,signal,time
mysub=subprocess.Popen("./cryInAbort", shell=True)
time.sleep(2)
mysub.send_signal(signal.SIGINT)
mysub.wait()
Run Code Online (Sandbox Code Playgroud) 在 python 中我打开了 4 个子进程。现在,当 python 脚本中出现新请求时,我想终止所有先前的进程。
我正在使用 python 2.7 和 windows 7 操作系统。
谢谢,
I have a subprocess which I open, which calls other processes.
I use os.killpg(os.getpgid(subOut.pid), signal.SIGTERM) to kill the entire group, but this kills the python script as well. Even when I call a python script with os.killpg from a second python script, this kills the second script as well. Is there a way to make os.killpg not stop the script?
Another solution would be to individually kill every child 1process. However, even using
p = psutil.Process(subOut.pid)
child_pid = p.children(recursive=True)
for …Run Code Online (Sandbox Code Playgroud)