Skip to main content

Implements EventEmitter using gevent

Project description

Latest version released on PyPi Test coverage Build status of master branch

This module implements EventEmitter with gevent.

Installation

To install the latest release from pypi:

pip install gevent-eventemitter

Usage

EventEmitter can be used as mixin, or on it’s own. Here is an example as mixin:

from eventemitter import EventEmitter

class MyClass(EventEmitter):
    pass

instance = MyClass()

Registering a callback

def do_stuff():
    print "Hello world!"

instance.on('my event', do_stuff)

instance.emit('my event')

Or as decorator

@instance.on('my event')
def do_stuff():
    print "Hello world!"

With once the callback will be called, you guessed it, only once.

@instance.once('my event')
def do_stuff(var):
    print "Hello %s!" % var

instance.emit('my event', 'Earth')  # arguments can be passed along events
instance.emit('my event')  # do_stuff won't be called
It’s possible to block wait for an event.

If there are event arguments they will be returned as a tuple

my_args = instance.wait_event('my event')
my_args = instance.wait_event('my event', timeout=5)  # wait at most 5seconds

On timeout wait_event will return None, or raise gevent.Timeout if raises=True

my_args = instance.wait_event('my event', timeout=5)
if my_args is None:
    print "Timeout!"

try:
    my_args = instance.wait_event('my event', timeout=5, raises=True)
except gevent.Timeout:
    print "Timeout!"

To remove a callback, or all callbacks.

instance.remove_listener('my event', do_stuff)
instance.remove_all_listeners()

Listening for None event will result in catching all events.

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

gevent-eventemitter-1.3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gevent_eventemitter-1.3-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file gevent-eventemitter-1.3.tar.gz.

File metadata

File hashes

Hashes for gevent-eventemitter-1.3.tar.gz
Algorithm Hash digest
SHA256 7321b5b301faf6ee4238f2a71290f350d70cec1544823761d709ee3dbe66d56d
MD5 9ce58ac2ad97a3c8637cfb4357145f22
BLAKE2b-256 77ef8b9e3e692dd0cc3ff060623e609397eacac8ed171f97b1e629f8d2c4d350

See more details on using hashes here.

File details

Details for the file gevent_eventemitter-1.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for gevent_eventemitter-1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a701c6a9b3f2d78506a50bdaee00d38413afc72e61d7255d586ff9285ad14028
MD5 4a100d4d1c8128ed722afe0959c15ad4
BLAKE2b-256 4f66eeb8dee07b2f48cab0718a567221bdc7ba98aec971576b346c9ebc5a9734

See more details on using hashes here.

Supported by

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