Skip to content

Instantly share code, notes, and snippets.

@phihag
Created October 2, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phihag/3820337 to your computer and use it in GitHub Desktop.
Save phihag/3820337 to your computer and use it in GitHub Desktop.
Demo that signals don't get spread
from __future__ import print_function
import signal
import subprocess
import os
import sys
import time
me = sys.argv[1] if len(sys.argv) >= 2 else 'controller'
signal.signal(signal.SIGUSR1, lambda *args: print(me + ': SIGUSR1 caught'))
assert os.path.exists(__file__)
if me == 'controller':
cpid = os.fork()
if cpid == 0:
me = 'parent'
print('PID of ' + me + ': ' + str(os.getpid()))
subprocess.call(['python', __file__, 'child'])
else:
print('PID of ' + me + ': ' + str(os.getpid()))
time.sleep(1)
os.kill(cpid, signal.SIGUSR1)
elif me == 'child':
print('PID of ' + me + ': ' + str(os.getpid()))
time.sleep(999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment