Skip to main content

A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution

Project description

python-eventemitter

Build status

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_eventemitter-1.0.13.tar.gz (15.9 kB view hashes)

Uploaded Source

Built Distribution

python_eventemitter-1.0.13-py3-none-any.whl (26.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page