【发布时间】:2014-11-21 14:20:59
【问题描述】:
在 Windows 中,我正在尝试创建一个等待 SIGINT 信号的 python 进程。当它接收到 SIGINT 时,我希望它只打印一条消息并等待再次出现 SIGINT。所以我使用了信号处理程序。
这是我的 signal_receiver.py 代码。
import signal, os, time
def handler(signum, frame):
print 'Yes , Received', signum
signal.signal(signal.SIGINT, handler)
print 'My process Id' , os.getpid()
while True:
print 'Waiting for signal'
time.sleep(10)
当这个进程运行时,我只是使用其他 python 进程向这个进程发送 SIGINT,
os.kill(pid,SIGINT).
但是当 signal_receiver.py 收到 SIGINT 时,它只是退出执行。但预期的行为是在处理函数中打印消息并继续执行。
有人可以帮我解决这个问题吗?这是 Windows 的限制吗,因为在 linux 中也可以正常工作。
提前致谢。
【问题讨论】:
-
Windows 没有信号。 Python 正在为您模拟它们,但可能不支持跨进程这样做。考虑使用一种本地 IPC 方法。
-
According to this answer, Python 不支持跨进程信号。
标签: python windows python-2.7 python-3.x signals