Events

Event handling is implemented via the Event and OneTime classes. In addition the EventHandler provide a mixin which can be used to attach events to a class. Event are implemented in the pulsar.async.events module.

Many times event

An event handler is created simply:

event = Event()

You can bind callbacks to an event:

def say_hello(arg, **kw):
    print('Hello %s!')

event.bind(say_hello)

and you can fire it:

>>> event.fire(None)
Hello None!
>>> event.fire('luca')
Hello luca!

An Event can be fired as many times as you like and therefore we referred to this type of event as a may times event.

One time event

As the name says, the OneTime is a special event which can be fired once only. Firing a OneTime event multiple times will cause an InavlidStateError to be raised.

A OneTime event is a specialised Future and therefore you can yield it in a coroutine to wait until it get fired.

Event handler

The EventHandler mixin adds event handling methods to python classes:

import pulsar

class Events(pulsar.EventHandler):
    ONE_TIME_EVENTS = ('start', 'finish')
    MANY_TIMES_EVENTS = ('data')

To fire an event, one uses the fire_event() method with first positional argument given by the event name:

>> o = Events()
>> o.fire_event('start')

Optionally, it is possible to pass one additional parameter:

>> o.fire_event('start', 'hello')

Adding event callbacks is done via the bind_event() method. The method accept two parameters, the event name and a callable accepting one parameters, the caller which fires the event or the optional positional parameter passed to the fire_event() method mentioned above:

def start_handler(result):
    ...

o.bind_event('start', start_handler)