OILS / demo / cpython / signals.sh View on Github | oils.pub

50 lines, 15 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# demo/cpython/signals.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -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
15print-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
21write-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?
26read-stdin() {
27 python -c 'import sys; sys.stdin.read()'
28}
29
30
31handler() {
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 '
35import signal
36import time
37
38def handler(signum, frame):
39 print(f"Signal {signum} received!")
40
41# Register handler for SIGINT (Ctrl+C)
42signal.signal(signal.SIGINT, handler)
43
44print("Sleeping for 5 seconds...")
45time.sleep(5)
46print("Done sleeping")
47'
48}
49
50"$@"