Skip to content

Instantly share code, notes, and snippets.

@billiepander
Last active February 26, 2026 05:46
Show Gist options
  • Select an option

  • Save billiepander/0a9349f2275c620c1611efd29e0babb7 to your computer and use it in GitHub Desktop.

Select an option

Save billiepander/0a9349f2275c620c1611efd29e0babb7 to your computer and use it in GitHub Desktop.
[Python] child process signal control
import os
import signal
import time
import sys
pid = os.getpid()
received = False
def signal_usr1(signum, frame):
"Callback invoked when a signal is received"
global received
received = True
print('CHILD {:>6}: Received USR1'.format(pid))
sys.stdout.flush()
print('CHILD {:>6}: Setting up signal handler'.format(pid))
sys.stdout.flush()
signal.signal(signal.SIGUSR1, signal_usr1)
print('CHILD {:>6}: Pausing to wait for signal'.format(pid))
sys.stdout.flush()
time.sleep(3)
if not received:
print('CHILD {:>6}: Never received signal'.format(pid))
PARENT : Pausing before sending signal...
CHILD 8117: Setting up signal handler
CHILD 8117: Pausing to wait for signal
PARENT : Signaling child
CHILD 8117: Received USR1
import os
import signal
import subprocess
import time
import sys
proc = subprocess.Popen(['python3', 'child_process.py'])
print('PARENT : Pausing before sending signal...')
sys.stdout.flush()
time.sleep(1)
print('PARENT : Signaling child')
sys.stdout.flush()
os.kill(proc.pid, signal.SIGUSR1) # !! here it rocks !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment