如何从python程序发送信号? [英] How can I send a signal from a python program?
本文介绍了如何从python程序发送信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码可以监听USR1信号
I have this code which listens to USR1 signals
import signal
import os
import time
def receive_signal(signum, stack):
print 'Received:', signum
signal.signal(signal.SIGUSR1, receive_signal)
signal.signal(signal.SIGUSR2, receive_signal)
print 'My PID is:', os.getpid()
while True:
print 'Waiting...'
time.sleep(3)
当我使用kill -USR1 pid
但是如何从上面的python脚本中发送相同的信号,以使其在10秒钟后自动发送USR1
并接收它,而无需打开两个终端进行检查?
But how can I send the same signal from within the above python script so that after 10 seconds it automatically sends USR1
and also receives it , without me having to open two terminals to check it?
推荐答案
您可以使用 os.kill():
os.kill(os.getpid(), signal.SIGUSR1)
将此代码放置在您想要发送信号的代码中的任何位置.
Put this anywhere in your code that you want to send the signal from.
这篇关于如何从python程序发送信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文