pyee is a Python library that provides an EventEmitter implementation, heavily inspired by Node.js's EventEmitter. It supports synchronous, asynchronous (asyncio, Twisted, Trio), and threaded event handling. The library is currently in version 13.0.1 and maintains a regular release cadence with major updates roughly annually and patch releases in between.
pip install pyeeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both the synchronous `EventEmitter` and the `AsyncIOEventEmitter` for handling events. It shows how to register listeners using the `@on` decorator and emit events using the `emit` method. For `AsyncIOEventEmitter`, it includes an example of waiting for handlers to complete and basic error handling.
Import the specific emitters from their respective submodules. For example, change `from pyee import AsyncIOEventEmitter` to `from pyee.asyncio import AsyncIOEventEmitter`.
Use `PyeeError` instead of `PyeeException`. `PyeeError` inherits from `PyeeException`.
Review type checking configurations and potentially update Twisted versions or adjust type ignore rules if encountering new type errors related to `twisted.python.Failure`.
Always attach an `error` listener (`@ee.on('error')`) to `EventEmitter` instances if unhandled exceptions are a concern, especially for synchronous handlers. For `AsyncIOEventEmitter`, be prepared to handle `error` events for exceptions originating in async handlers.Install the library using pip: `pip install pyee`
Import `EventEmitter` from `pyee.base` for the basic emitter, or from specific submodules like `pyee.asyncio` for an asyncio-compatible emitter: `from pyee.base import EventEmitter` (for synchronous) or `from pyee.asyncio import AsyncIOEventEmitter` (for asyncio).
Replace `from pyee import EventEmitter` with `from pyee.base import EventEmitter` for a basic synchronous emitter, or use a specialized emitter like `from pyee.asyncio import AsyncIOEventEmitter` if working with `asyncio`.
Upgrade `pyee` to a version that officially supports Python 3.12. For example, `pyee` version 12.0.0 and above addresses this compatibility issue. Use `pip install --upgrade pyee`.