General purpose event handling routines

class sdl2.ext.EventHandler(sender)[source]

A simple event handling class, which manages callbacks to be executed.

The EventHandler does not need to be kept as separate instance, but is mainly intended to be used as attribute in event-aware class objects.

>>> def myfunc(sender):
...     print("event triggered by %s" % sender)
...
>>> class MyClass(object):
...     def __init__(self):
...         self.anevent = EventHandler(self)
...
>>> myobj = MyClass()
>>> myobj.anevent += myfunc
>>> myobj.anevent()
event triggered by <__main__.MyClass object at 0x801864e50>
callbacks

A list of callbacks currently bound to the EventHandler.

sender

The responsible object that executes the EventHandler.

add(callback : Callable)[source]

Adds a callback to the EventHandler.

remove(callback : Callable)[source]

Removes a callback from the EventHandler.

__call__(*args) → [ ... ][source]

Executes all connected callbacks in the order of addition, passing the sender of the EventHandler as first argument and the optional args as second, third, … argument to them.

This will return a list containing the return values of the callbacks in the order of their execution.

class sdl2.ext.MPEventHandler(sender)[source]

An asynchronous event handling class based on EventHandler, in which callbacks are executed in parallel. It is the responsibility of the caller code to ensure that every object used maintains a consistent state. The MPEventHandler class will not apply any locks, synchronous state changes or anything else to the arguments or callbacks being used. Consider it a “fire-and-forget” event handling strategy.

Note

The MPEventHandler relies on the multiprocessing module. If the module is not available in the target environment, a sdl2.ext.compat.UnsupportedError is raised.

Also, please be aware of the restrictions that apply to the multiprocessing module; arguments and callback functions for example have to be pickable, etc.

__call__(*args) → AsyncResult[source]

Executes all connected callbacks within a multiprocessing.pool.Pool, passing the sender as first argument and the optional args as second, third, … argument to them.

This will return a multiprocessing.pool.AsyncResult containing the return values of the callbacks in the order of their execution.