diff --git a/poc/py/stream_to_state.py b/poc/py/stream_to_state.py index 29939b3..bf6ed11 100644 --- a/poc/py/stream_to_state.py +++ b/poc/py/stream_to_state.py @@ -27,15 +27,27 @@ def with_(N, R, T, p): bucket = Bucket(N, R, T, cb.cb()) - while readline(): - bucket.push() + while True: + got = readline() + if got: + bucket.push() + else: + bucket.pop() def readline(): - try: - line = input() - return True - except EOFError: - return False + def __input(*args): + return input() + def _input(*args): + try: + foo = __input() + except Exception as e: + foo = None + return foo + timeout = 1 + signal.signal(signal.SIGALRM, __input) + signal.alarm(timeout) + foo = _input() + return not foo is None class Bucket: def __init__(self, N, R, T, CB): diff --git a/poc/py/test.sh b/poc/py/test.sh index 726028d..cd9b227 100644 --- a/poc/py/test.sh +++ b/poc/py/test.sh @@ -6,16 +6,16 @@ set -o pipefail cleanup() { kill -9 $(jobs -p) || true } -trap cleanup EXIT +trap cleanup EXIT python3 ./state_to_buttons.py & ( - while echo ""; do - if [ 0 == $((RANDOM%4)) ]; then + while echo ""; do + if [ 0 == $((RANDOM%4)) ]; then sleep 3 fi date >&2 done -) | python3 ./stream_to_state.py +) | python3 ./stream_to_state.py