From a37602e6667853cd3d4e63a38afaff688ea0a3cf Mon Sep 17 00:00:00 2001 From: taizan-hokouto <55448286+taizan-hokuto@users.noreply.github.com> Date: Tue, 3 Nov 2020 11:57:24 +0900 Subject: [PATCH] Fix keyboard interrupt process --- pytchat/core_async/livechat.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pytchat/core_async/livechat.py b/pytchat/core_async/livechat.py index a7c4371..5dccb4c 100644 --- a/pytchat/core_async/livechat.py +++ b/pytchat/core_async/livechat.py @@ -111,8 +111,7 @@ class LiveChatAsync: self._set_exception_handler(exception_handler) if interruptable: signal.signal(signal.SIGINT, - (lambda a, b: asyncio.create_task( - LiveChatAsync.shutdown(None, signal.SIGINT, b)))) + (lambda a, b: self._keyboard_interrupt())) self._setup() def _setup(self): @@ -325,7 +324,11 @@ class LiveChatAsync: self._is_alive = False self._buffer.put_nowait({}) self.processor.finalize() - + + def _keyboard_interrupt(self): + self.exception = exceptions.ChatDataFinished() + self.terminate() + def _task_finished(self): ''' Terminate fetching chats. @@ -348,15 +351,3 @@ class LiveChatAsync: def _set_exception_handler(cls, handler): loop = asyncio.get_event_loop() loop.set_exception_handler(handler) - - @classmethod - async def shutdown(cls, event, sig=None, handler=None): - cls._logger.debug("shutdown...") - tasks = [t for t in asyncio.all_tasks() if t is not - asyncio.current_task()] - [task.cancel() for task in tasks] - - cls._logger.debug("complete remaining tasks...") - await asyncio.gather(*tasks, return_exceptions=True) - loop = asyncio.get_event_loop() - loop.stop()