我正在使用python来管理一些模拟。我建立的参数和运行程序使用:如何阻止python传播信号到子进程?
pipe = open('/dev/null', 'w')
pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe)
我的代码处理不同的信号。 Ctrl + C将停止模拟,询问我是否要保存并正常退出。我有其他信号处理程序(例如强制数据输出)。
我想要的是向我的python脚本发送一个信号(SIGINT,Ctrl + C),它会询问用户要发送给程序的信号。
防止代码工作的唯一的事情是,它似乎不管我做什么,按Ctrl + C将“转”给子:代码将它捉住并退出:
try:
<wait for available slots>
except KeyboardInterrupt:
print "KeyboardInterrupt catched! All simulations are paused. Please choose the signal to send:"
print " 0: SIGCONT (Continue simulation)"
print " 1: SIGINT (Exit and save)"
[...]
answer = raw_input()
pid.send_signal(signal.SIGCONT)
if (answer == "0"):
print " --> Continuing simulation..."
elif (answer == "1"):
print " --> Exit and save."
pid.send_signal(signal.SIGINT)
[...]
所以无论我做什么,该程序正在接收SIGINT,我只想让我的Python脚本看到。我怎样才能做到这一点???
我也试过:
signal.signal(signal.SIGINT, signal.SIG_IGN)
pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe)
signal.signal(signal.SIGINT, signal.SIG_DFL)
运行的程序,但是这给了相同的结果:程序捕捉SIGINT。
Thanx!
谢谢你的建议。我认为这对我的目标来说太复杂了。基本上,我只想暂停父母脚本,询问用户并向所有子进程发送信号。也许另一个键盘输入可能会暂停父母脚本,如ctrl + x? – 2010-09-26 16:02:20
是的,正如我所说的,我不会推荐这个解决方案。如果您在单独的线程中监听键盘事件,则可以使用任何您想要暂停父项的组合键。 – 2010-09-26 22:10:07