A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution
Project description
python-eventemitter
A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution
Help
See documentation for more details
Install
To install python-eventemitter, simply use pip:
$ pip install python-eventemitter
Usage
Synchronous API
from eventemitter import EventEmitter
def 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
import asyncio
from 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())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_eventemitter-1.0.13.tar.gz.
File metadata
- Download URL: python_eventemitter-1.0.13.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35372f6a0613eb786bc158c6b2ba70167c0b82d37940b5517a0f43ea706b977f
|
|
| MD5 |
f017ced532d4b04e1267ebc8ee26d904
|
|
| BLAKE2b-256 |
deaa5fbf8add73233ab8020b760b57e8313371512b3528d7d6ec63095d281458
|
File details
Details for the file python_eventemitter-1.0.13-py3-none-any.whl.
File metadata
- Download URL: python_eventemitter-1.0.13-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96a01cd3b64e3d11e1b4e8521447b24d8081b359d3641de306d7ed8ce66f71ac
|
|
| MD5 |
fb0382cd1bc2ff216936d6320489687e
|
|
| BLAKE2b-256 |
f3136090fbbc0b84b58b5538f33a04dc03adfd53af098e391c8ea3c857401405
|