| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Usage:
|
| 4 | # demo/cpython/signals.sh <function name>
|
| 5 |
|
| 6 | set -o nounset
|
| 7 | set -o pipefail
|
| 8 | set -o errexit
|
| 9 |
|
| 10 | # I get EPIPE, but I don't get KeyboardInterrupt.
|
| 11 | # Which calls can raise KeyboardInterrupt?
|
| 12 |
|
| 13 | # https://stackoverflow.com/questions/35571440/when-is-keyboardinterrupt-raised-in-python
|
| 14 |
|
| 15 | print-pipe() {
|
| 16 | # This should fill up a pipe.
|
| 17 | # Can't seem to get KeyboardInterrupt
|
| 18 | python -c 'print "x"*100000' | sleep 1
|
| 19 | }
|
| 20 |
|
| 21 | write-pipe() {
|
| 22 | python -c 'import sys; sys.stdout.write("x"*100000)' | sleep 1
|
| 23 | }
|
| 24 |
|
| 25 | # The easiest way to see KeyboardInterrupt. read and write are not symmetric?
|
| 26 | read-stdin() {
|
| 27 | python -c 'import sys; sys.stdin.read()'
|
| 28 | }
|
| 29 |
|
| 30 |
|
| 31 | handler() {
|
| 32 | # Based on this behavior, I think 'builtin sleep' should run signal handlers
|
| 33 | # We could have a flag to turn it off
|
| 34 | python3 -c '
|
| 35 | import signal
|
| 36 | import time
|
| 37 |
|
| 38 | def handler(signum, frame):
|
| 39 | print(f"Signal {signum} received!")
|
| 40 |
|
| 41 | # Register handler for SIGINT (Ctrl+C)
|
| 42 | signal.signal(signal.SIGINT, handler)
|
| 43 |
|
| 44 | print("Sleeping for 5 seconds...")
|
| 45 | time.sleep(5)
|
| 46 | print("Done sleeping")
|
| 47 | '
|
| 48 | }
|
| 49 |
|
| 50 | "$@"
|