Skip to main content

EventSail

codecov PyPI - Implementation PyPI - Wheel PyPI - Downloads PyPI - Status

EventSail is a minimal observer pattern package which is performant as well as supports both sync and async operations. Born out of utility and curiosity to replicate Javascript EventEmitter, here we are with library with similar functionalities.

codecov graph

Usage

Installation

Package can be installed from PyPi using below command.

python -m pip install eventsail

If using poetry or pipenv for packaging, use below command.

poetry add eventsail
pipenv install eventsail

Info

EventSail has a very minimal configuration to get started. It exports Event class which consumes Emitter under the hood for firing callbacks for the events. For easing, creation of Event, EventSail also exports event method for instantiation.

from eventsail import event, Event
test_event: Event = event('test_event_name')

Resultant test_event will be a synchronous event type. If you want an asynchronous event, you can instantiate it as.

test_event: Event = event("test", is_sync=False)

Now test_event would be of type asynchronous event.

Subscribing a callback

To register callbacks/handlers, you can either call subscribe method on Event instance passing callback/function or you can also use it as decorator over your method.

def foo(*args,**kwargs): ...
test_event.subscribe(foo)

or

@test_event.subscribe
def foo(*args,**kwargs): ...

For Async event, all the callbacks will be delegated to threads thus preventing any operations to be blocked.

If you want to leverage power of async/await you will have to hint Event class to do so by passing use_asyncio flag to True.

test_event = event("test", is_sync=False, use_asyncio=True)

This way it will entertain coroutines as well by creating Task and placing them over running event loop. For non coroutine methods, it will execute them over loop in a Thread Pool executor, thus preventing blocking of Event loop.

If you want to call a callback/listener only once, you can use once method exposed by Event object. Post trigger, listener will be automatically unsubscribed.

@test_event.once
def foo(*args,**kwargs): ...

Above callback will only be fired once.

Unsubscribing a callback

To unsubscribe a callback, you can simply call unsubscribe method on Object passing callback you want to unsubscribe.

test_event.unsubscribe(abc)

Calling this method on already unsubscribed callback would do no harm.

To remove all the listeners/callbacks, you can call clear method.

test_event.clear()

Event class is also singleton class, so you will always get same instance for same set of arguments. This is intentionally kept in place so that we do not loose listeners subscribed.

Waiting for async tasks

There can be instances where we would need to wait for async emitted tasks to complete before shutdown as this can be critical. Event class exposes certain properties containing async tasks.

  • Event.all_async_tasks : This will return all the async tasks scheduled over Event loop for the Emitter.
  • Event.own_async_tasks : This will only return async tasks corresponding to the immediate parent Emitter of the Event.

To wait for async tasks to complete, Event class exposes awaitable API to wait for tasks to complete. This can be called as below.

await test_event.wait_for_async_tasks()

This will wait for all the async tasks. This also has a default timeout for 10 seconds post which it will error out.

Testing

To run tests, simply run below command:

pytest -s --cov=.

License

EventSail is released under the MIT License. See the bundled LICENSE file for details.

Metadata

Release files for eventsail 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for eventsail 0.1.2
File Size Uploaded
eventsail-0.1.2.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for eventsail 0.1.2
File Interpreter ABI Platform
eventsail-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / eventsail-0.1.2.tar.gz

Download URL eventsail-0.1.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
745b5724fd7a37cb433687046d5d023f471a7dfddb2aee7471cf809dd4f510ce
BLAKE2b-256 checksum
How to use checksums
1b6d06c46147dce450f784cd0d73712a97f1bfb0443c511a55bb22b983b26313
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / eventsail-0.1.2-py3-none-any.whl

Download URL eventsail-0.1.2-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e48c2ceffa837a0209c1a44d24018ca1c947f70653c9452ffecbce4dc2d16101
BLAKE2b-256 checksum
How to use checksums
b7ae958e7233a8f23605b3727d1b9fb23ffcdac9efcd38ff6eec5a7b930d7333
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page