Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

控制Listener停止过程中报错 #6

Closed
adk23333 opened this issue Feb 11, 2023 · 2 comments
Closed

控制Listener停止过程中报错 #6

adk23333 opened this issue Feb 11, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@adk23333
Copy link
Contributor

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

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'?
@lumina37
Copy link
Owner

lumina37 commented Feb 11, 2023

__aexit__替代close 69eb87f

@lumina37 lumina37 added the bug Something isn't working label Feb 11, 2023
@adk23333
Copy link
Contributor Author

__aexit__替代close 69eb87f

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants