我创建了两个简单的脚本:
script.py:
import time
import sys
import signal
try:
print('i am running')
time.sleep(10)
print('i am done')
except KeyboardInterrupt:
print("you don't like me??")和test.py:
import subprocess
import signal
from threading import Thread
import time
import os
p = subprocess.Popen('python script.py', shell=True)
t = Thread(target=p.wait)
t.start()
print('sleeping')
time.sleep(2)
print('interrupt')
p.send_signal(signal.SIGINT)
#p.send_signal(signal.SIGTERM)
t.join()
print('process finished')如果我运行test.py (在ubuntu上),预期的结果将是:
sleeping
i am running
interrupt
you don't like me??
process finished相反,SIGINT似乎被忽略了:
sleeping
i am running
interrupt
i am done
process finishedSIGTERM按预期终止该过程。但是,没有引发KeyboardInterrupt。
即使我在script.py中添加了以下行
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
signal.signal(signal.SIGINT, signal_handler)似乎没有收到SIGINT。
但是,当我自己按C+CTRL时,就会收到一个SIGINT。但这对我来说不是一个选择,因为SIGINT一定是时间触发的。
有人知道为什么会发生这种事吗?
干杯,托马斯
发布于 2018-01-17 15:14:19
(我已经在示例中删除了线程的使用,因为它除了增加代码行之外没有向示例添加任何内容)
这与如何在流程组中处理信号有关,您可能会发现另一个如此回答的答案很有帮助。
import subprocess
import signal
import time
import os
p = subprocess.Popen('python script.py', shell=True, preexec_fn=os.setsid)
print('sleeping')
time.sleep(2)
os.killpg(os.getpgid(p.pid), signal.SIGINT)
print('interrupt')
p.wait()
print('process finished')这将产生预期的结果:
andy@batman[14:58:04]:~/so$ python test.py
sleeping
i am running
interrupt
you don't like me??
process finished信号由流程组处理,因此从流程组内的进程发送信号并不像您想的那样工作。
有趣的是,如果您不使用shell=True (如果你能避免的话,你就不应该用),它就能正常工作。
import subprocess
import signal
import time
import os
p = subprocess.Popen(['python', 'script.py'])
print('sleeping')
time.sleep(2)
p.send_signal(signal.SIGINT)
print('interrupt')
p.wait()
print('process finished')所以,如果我诚实的话,这个答案有点垃圾,因为我可以给你们看两件表面上可行的东西,但没有真正解释原因。
https://stackoverflow.com/questions/48298894
复制相似问题