From 7ea1612b326a40ac0f8e607390a8306f51cf7a68 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 19 Dec 2019 10:53:20 -0500 Subject: [PATCH] Gracefully handle sigint --- run.py | 12 ++++++++++-- util.py | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/run.py b/run.py index 26a58c8..fcf4e62 100644 --- a/run.py +++ b/run.py @@ -153,6 +153,8 @@ def publish_worker(queue: Queue, helper, p): while True: try: item, board = queue.get() + if item is None: + break publish(item, board, helper, channel, web) except Exception as e: @@ -243,9 +245,15 @@ if __name__ == "__main__": publish_q = Queue() for _ in range(5): publish_thread = Thread(target=publish_worker, args=(publish_q, chan_helper, proxy)) + publish_thread.setDaemon(True) publish_thread.start() s = ChanScanner(chan_helper, proxy) while True: - for p, b in s.all_posts(): - publish_q.put((p, b)) + try: + for p, b in s.all_posts(): + publish_q.put((p, b)) + except KeyboardInterrupt as e: + for _ in range(5): + publish_q.put((None, None)) + break diff --git a/util.py b/util.py index b501173..91af7c4 100644 --- a/util.py +++ b/util.py @@ -70,6 +70,8 @@ class Web: }, }]) return r + except KeyboardInterrupt as e: + raise e except Exception as e: logger.error(str(e) + traceback.format_exc()) if self.monitoring: