Last active
February 20, 2016 17:11
-
-
Save zed/215a57b3681cc5f77d2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Called from parent.py""" | |
import sys | |
import time | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: # handle SIGINT | |
sys.exit('child exits on KeyboardInterrupt') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Show that child.wait() hangs in the sighandler. | |
Example: | |
$ python3 parent.py & | |
$ kill -USR1 $! | |
child exits on KeyboardInterrupt | |
From parent 1 | |
The example shows that send_signal() doesn't hang. | |
If you run `python parent.py --call-wait &` then the parent hangs in the | |
signal handler after `kill -USR1 $!` command on child.wait() call. | |
Running `python parent.py --call-waitpid &` and then `kill -USR1 $!` | |
shows that os.waitpid() does NOT hang in the signal handler but the parent | |
prints `From parent 0` instead of `From parent 1` -- wrong child's exit status. | |
""" | |
import os | |
import signal | |
import subprocess | |
import sys | |
def sighandler(*args): | |
child.send_signal(signal.SIGINT) | |
if '--call-wait' in sys.argv: | |
child.wait() # It hangs on Python 3 due to child._waitpid_lock | |
if '--call-waitpid' in sys.argv: | |
rc = os.waitpid(child.pid, 0) | |
raise SystemExit("From parent's signal hander: {}".format(rc)) | |
signal.signal(signal.SIGUSR1, sighandler) | |
child = subprocess.Popen([sys.executable, 'child.py']) | |
sys.exit("From parent %d" % child.wait()) # return child's exit status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment