From 5049d97a24fdd3a18fbc914cdcdf11e2181d81c9 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 11 Apr 2022 07:28:19 -0600 Subject: [PATCH] stream reads every 1s, even if nothing, so pop can occur regularly --- poc/py/stream_to_state.py | 26 +++++++++++++++++++------- poc/py/test.sh | 8 ++++---- 2 files changed, 23 insertions(+), 11 deletions(-) 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