https://github.com/jonghwanhyeon/python-eventemitter
A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution
https://github.com/jonghwanhyeon/python-eventemitter
Last synced: 3 months ago
JSON representation
A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution
- Host: GitHub
- URL: https://github.com/jonghwanhyeon/python-eventemitter
- Owner: jonghwanhyeon
- License: mit
- Created: 2023-03-25T09:42:06.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-06T11:26:11.000Z (11 months ago)
- Last Synced: 2025-03-24T05:45:41.761Z (3 months ago)
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python-eventemitter
A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution
## Help
See [documentation](https://python-eventemitter.readthedocs.io) for more details## Install
To install **python-eventemitter**, simply use pip:```console
$ pip install python-eventemitter
```## Usage
### Synchronous API
```python
from eventemitter import EventEmitterdef main():
ee = EventEmitter()sounds: list[str] = []
ee.add_listener("sound", lambda: sounds.append("woof"))
ee.prepend_listener("sound", lambda: sounds.append("meow"))
ee.on("sound", lambda: sounds.append("oink"))@ee.on("sound")
def from_cow():
sounds.append("moo")@ee.once("sound")
def from_bee():
sounds.append("buzz")ee.emit("sound") # Run events in order
print(sounds) # ['meow', 'woof', 'oink', 'moo', 'buzz']if __name__ == "__main__":
main()
```### Asynchronous API
``` python
import asynciofrom eventemitter import AsyncIOEventEmitter
async def main():
aee = AsyncIOEventEmitter()sounds: set[str] = set()
aee.add_listener("sound", lambda: sounds.add("woof"))
aee.prepend_listener("sound", lambda: sounds.add("meow"))
aee.on("sound", lambda: sounds.add("oink"))@aee.on("sound")
def from_cow():
sounds.add("moo")@aee.once("sound")
async def from_bee():
sounds.add("buzz")await aee.emit("sound") # Run events concurrently
print(sounds) # {'woof', 'meow', 'buzz', 'moo', 'oink'}if __name__ == "__main__":
asyncio.run(main())
```