jfhbrook / pyee

A rough port of Node.js's EventEmitter to Python with a few tricks of its own
https://github.com/jfhbrook/public
MIT License
362 stars 37 forks source link

async lock error #119

Closed smallevilbeast closed 1 year ago

smallevilbeast commented 1 year ago
        _client.on('event_server_push', self._on_server_push)
  File "E:\public_project\imchat-new\venv\lib\site-packages\pyee\base.py", line 77, in on
    return self.add_listener(event, f)
  File "E:\public_project\imchat-new\venv\lib\site-packages\pyee\base.py", line 108, in add_listener
    self._add_event_handler(event, f, f)
  File "E:\public_project\imchat-new\venv\lib\site-packages\pyee\base.py", line 113, in _add_event_handler
    self.emit("new_listener", event, k)
  File "E:\public_project\imchat-new\venv\lib\site-packages\pyee\base.py", line 176, in emit
    handled = self._call_handlers(event, args, kwargs)
  File "E:\public_project\imchat-new\venv\lib\site-packages\pyee\base.py", line 151, in _call_handlers
    with self._lock:
  File "E:\public_project\imchat-new\venv\lib\site-packages\tornado\locks.py", line 553, in __enter__
    raise RuntimeError("Use `async with` instead of `with` for Lock")
RuntimeError: Use `async with` instead of `with` for Lock
smallevilbeast commented 1 year ago

example:

import pyee
import asyncio
from asyncio.locks import Lock

class Demo(pyee.AsyncIOEventEmitter):
    def __init__(self):
        super().__init__()
        self._lock = Lock()

    async def get_seq(self):
        async with self._lock:
            return 1

    async def send(self):
        seq = await self.get_seq()
        self.emit('event_test')

class LockTest(pyee.AsyncIOEventEmitter):
    def __init__(self):
        super().__init__()

        self._demo = Demo()
        self._demo.on('event_test', self._on_event_test)

    async def _on_event_test(self):
        print('_on_event_test')

    async def send(self):
        await self._demo.send()

async def run():
    test = LockTest()
    await test.send()

if __name__ == '__main__':
    asyncio.run(run())