Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pyee._base import BaseEventEmitter
from warnings import warn
try:
from asyncio import iscoroutine, ensure_future
except ImportError:
iscoroutine = None
ensure_future = None
class CompatEventEmitter(BaseEventEmitter):
"""An EventEmitter exposed for compatibility with prior versions of
pyee. This functionality is deprecated; you should instead use either
``AsyncIOEventEmitter``, ``TwistedEventEmitter``, ``ExecutorEventEmitter``,
``TrioEventEmitter`` or ``BaseEventEmitter``.
This class is similar to the ``AsyncIOEventEmitter`` class, but also allows
for overriding the scheduler function (``ensure_future`` by default as in
``ASyncIOEventEmitter``) and does duck typing checks to handle Deferreds.
In other words, by setting ``scheduler`` to
``twisted.internet.defer.ensureDeferred`` this will support twisted use
cases for coroutines.
When calling synchronous handlers, raised exceptions are ignored - as with
the BaseEventEmitter, you must capture and handle your own exceptions.
However, for coroutine functions, exceptions are handled by emitting them
on the ``error`` event. Note that when using with twisted, the ``error``
# -*- coding: utf-8 -*-
from asyncio import ensure_future, Future, iscoroutine
from pyee._base import BaseEventEmitter
__all__ = ['AsyncIOEventEmitter']
class AsyncIOEventEmitter(BaseEventEmitter):
"""An event emitter class which can run asyncio coroutines in addition to
synchronous blocking functions. For example::
@ee.on('event')
async def async_handler(*args, **kwargs):
await returns_a_future()
On emit, the event emitter will automatically schedule the coroutine using
``asyncio.ensure_future`` and the configured event loop (defaults to
``asyncio.get_event_loop()``).
Unlike the case with the BaseEventEmitter, all exceptions raised by
event handlers are automatically emitted on the ``error`` event. This is
important for asyncio coroutines specifically but is also handled for
synchronous functions for consistency.