Fix keyboard interrupt process

This commit is contained in:
taizan-hokouto
2020-11-03 11:57:24 +09:00
parent 306b69198e
commit a37602e666

View File

@@ -111,8 +111,7 @@ class LiveChatAsync:
self._set_exception_handler(exception_handler) self._set_exception_handler(exception_handler)
if interruptable: if interruptable:
signal.signal(signal.SIGINT, signal.signal(signal.SIGINT,
(lambda a, b: asyncio.create_task( (lambda a, b: self._keyboard_interrupt()))
LiveChatAsync.shutdown(None, signal.SIGINT, b))))
self._setup() self._setup()
def _setup(self): def _setup(self):
@@ -326,6 +325,10 @@ class LiveChatAsync:
self._buffer.put_nowait({}) self._buffer.put_nowait({})
self.processor.finalize() self.processor.finalize()
def _keyboard_interrupt(self):
self.exception = exceptions.ChatDataFinished()
self.terminate()
def _task_finished(self): def _task_finished(self):
''' '''
Terminate fetching chats. Terminate fetching chats.
@@ -348,15 +351,3 @@ class LiveChatAsync:
def _set_exception_handler(cls, handler): def _set_exception_handler(cls, handler):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.set_exception_handler(handler) 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()