Python 3.3+Linux · macOSsignalfunction
signal.pthread_kill()
sending signals
Sends a signal to a specific thread of the own process by its identifier. Unix only (Linux, macOS).
Syntax
signal.pthread_kill(thread_id, signalnum)
The Python handler still runs in the main thread, not in the target one: the signal only interrupts that thread's system call (InterruptedError at the C level). You cannot stop a thread running Python code this way. Signal 0 sends nothing and only checks whether the thread is alive. The identifier is threading.get_ident() or Thread.ident, not native_id.
Examples
import signal, threading
seen = []
def handler(signum, frame):
seen.append(threading.current_thread().name)
old = signal.signal(signal.SIGUSR1, handler)
signal.pthread_kill(threading.get_ident(), signal.SIGUSR1)
print(seen) # → ['MainThread']
print(signal.pthread_kill(threading.get_ident(), 0)) # → None — поток жив
signal.signal(signal.SIGUSR1, old)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy