Sunday, January 18, 2009

boduch.event.EventManager

The boduch Python library provides publish/subscribe event functionality. At the heart of this functionality is the EventManager class. This class is meant to be static in the strictest sense; there should never be EventManager instances. This makes sense if you are using the library in several modules. Some modules may publish events while others may subscribe to them. There needs to be a central place to store subscriptions. This is solved by static class attributes and methods.

Here is a visualization of what the EventManager class looks like.



Every subscription is stored in the subscriptions attribute. The threaded attribute determines if the EventManager is running in threaded mode.

The get_subscriptions() method will return a list of handles corresponding to the specified event. If no event is specified, all subscriptions are returned.

The get_threaded() method simply returns the threaded attribute of the EventManager.

The publish() method will publish the specified event. The keyword parameters are passed along to each handler that is built for the event.

The subscribe() method attaches a handle to a specific event type.

The unsubscribe() method detaches a handle from a specific event type.

The prioritize() method sorts the handles attached to the specified event numerically. That is, the higher the sum of any given handle, the more likely it is to be executed first when attached to an event.

The build_handlers() method will instantiate all handles attached to the specified event.

Finally, start_event_thread() will start a new thread of control. Within, each specified handle is executed.

No comments :

Post a Comment