lumina37/aiotieba-reviewer

控制Listener停止过程中报错

Closed this issue · 2 comments

代码如下
大体上是照着示例写的

class HandleThread(Thread):
    def __init__(self):
        super(HandleThread, self).__init__()
        self._stop_event = Event()

    def run(self):
        asyncio.run(self.handler())

    async def handler(self):
        async with MyListener(self) as listener:
            await listener.run()

    def stop(self):
        self._stop_event.set()

    def stopped(self):
        return self._stop_event.is_set()


class MyListener(Listener):
    def __init__(self, handle: HandleThread):
        super(MyListener, self).__init__()
        self.Handle = handle

    async def run(self) -> None:
        while not self.Handle.stopped():
            try:
                tb.LOG().debug('heartbeat')
                asyncio.create_task(self._fetch_and_execute_cmds())
                await asyncio.sleep(5)

            except asyncio.CancelledError:
                return
            except Exception:
                tb.LOG().critical("Unhandled error", exc_info=True)
                return

报错如下

Exception in thread Thread-2:
Traceback (most recent call last):
  File "D:\Python3.10.6\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "E:\pythonProject\checkID\tieba\cmd_handler.py", line 846, in run
    asyncio.run(self.handler())
  File "D:\Python3.10.6\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "D:\Python3.10.6\lib\asyncio\base_events.py", line 646, in run_until_complete
    return future.result()
  File "E:\pythonProject\checkID\tieba\cmd_handler.py", line 849, in handler
    async with MyListener(self) as listener:
  File "E:\pythonProject\checkID\tieba\cmd_handler.py", line 258, in __aexit__
    await self.close()
  File "E:\pythonProject\checkID\tieba\cmd_handler.py", line 246, in close
    *[c.close() for c in itertools.chain.from_iterable(self.admins.values())], self.listener.close()
  File "E:\pythonProject\checkID\tieba\cmd_handler.py", line 246, in <listcomp>
    *[c.close() for c in itertools.chain.from_iterable(self.admins.values())], self.listener.close()
AttributeError: 'Client' object has no attribute 'close'. Did you mean: 'core'?

__aexit__替代close 69eb87f

__aexit__替代close 69eb87f

thx