如何从python程序发送信号?

时间:2022-04-19 02:12:50

I have this code which listens to USR1 signals

我有这个代码可以监听USR1信号

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)

This works when I send signals with kill -USR1 pid

当我使用kill -USR1 pid发送信号时,这种方法有效

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?

但是我如何从上面的python脚本中发送相同的信号,以便在10秒后自动发送USR1并接收它,而不必打开两个终端来检查它?

2 个解决方案

#1


27  

You can use os.kill():

你可以使用os.kill():

os.kill(os.getpid(), signal.SIGUSR1)

Put this anywhere in your code that you want to send the signal from.

将它放在您想要发送信号的代码中的任何位置。

#2


5  

If you are willing to catch SIGALRM instead of SIGUSR1, try:

如果您愿意捕获SIGALRM而不是SIGUSR1,请尝试:

signal.alarm(10)

Otherwise, you'll need to start another thread:

否则,您需要启动另一个线程:

import time, os, signal, threading
pid = os.getpid()
thread = threading.Thread(
  target=lambda: (
    time.sleep(10),
    os.kill(pid, signal.SIGUSR1)))
thread.start()

Thus, this program:

因此,这个程序:

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)
signal.signal(signal.SIGALRM, receive_signal)  # <-- THIS LINE ADDED

print 'My PID is:', os.getpid()

signal.alarm(10)                               # <-- THIS LINE ADDED

while True:
    print 'Waiting...'
    time.sleep(3)

produces this output:

产生这个输出:

$ python /tmp/x.py 
My PID is: 3029
Waiting...
Waiting...
Waiting...
Waiting...
Received: 14
Waiting...
Waiting...

#1


27  

You can use os.kill():

你可以使用os.kill():

os.kill(os.getpid(), signal.SIGUSR1)

Put this anywhere in your code that you want to send the signal from.

将它放在您想要发送信号的代码中的任何位置。

#2


5  

If you are willing to catch SIGALRM instead of SIGUSR1, try:

如果您愿意捕获SIGALRM而不是SIGUSR1,请尝试:

signal.alarm(10)

Otherwise, you'll need to start another thread:

否则,您需要启动另一个线程:

import time, os, signal, threading
pid = os.getpid()
thread = threading.Thread(
  target=lambda: (
    time.sleep(10),
    os.kill(pid, signal.SIGUSR1)))
thread.start()

Thus, this program:

因此,这个程序:

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)
signal.signal(signal.SIGALRM, receive_signal)  # <-- THIS LINE ADDED

print 'My PID is:', os.getpid()

signal.alarm(10)                               # <-- THIS LINE ADDED

while True:
    print 'Waiting...'
    time.sleep(3)

produces this output:

产生这个输出:

$ python /tmp/x.py 
My PID is: 3029
Waiting...
Waiting...
Waiting...
Waiting...
Received: 14
Waiting...
Waiting...