I am trying use the AsyncIOEventEmitter
from the pyee library without success. For some reason the emitted event "Hi" never reaches the async_handler
to complete the asyncio future. I also did not find proper examples online. Additionally I tried providing the current event and using a new event loop for the AsyncIOEventEmitter
, but both yields the same result.
Can someone help me out? Example unit test below:
import asyncio
import logging
import pytest
from pyee import AsyncIOEventEmitter
LOG = logging.getLogger(__name__)
@pytest.mark.asyncio
async def test_setup(event_loop):
LOG.info("1 - start")
event_emitter = AsyncIOEventEmitter(asyncio.new_event_loop())
# Create a new Future object.
future_result = event_loop.create_future()
LOG.info("2 - emit event")
event_emitter.emit("event", "Hi")
@event_emitter.on("event")
async def async_handler(message):
LOG.info(">>> %s", message)
future_result.set_result(message)
return future_result
# Wait until *future_result* has a result and print it.
LOG.info(await future_result)
Thanks!
ok figured it out, the async_handler
method must be defined earlier in the test...
This worked now:
"""Event emitter playground"""
import asyncio
import logging
import pytest
from pyee import AsyncIOEventEmitter
LOG = logging.getLogger(__name__)
@pytest.mark.asyncio
async def test_setup(event_loop):
"""Receive event from emitter and complete future!"""
LOG.info("1 - start")
event_emitter = AsyncIOEventEmitter(asyncio.new_event_loop())
@event_emitter.on("event")
def async_handler(message):
LOG.info(">>> %s", message)
future_result.set_result(message)
future_result = event_loop.create_future()
LOG.info("2 - emit event")
event_emitter.emit("event", "Hi")
LOG.info(await future_result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.