My favorites | Sign in
Project Logo
                
Search
for
Updated Nov 06, 2008 by bowman.joseph
Labels: Featured
Event  

Event

A simple publish/subscribe based event dispatcher It sets itself to the main function. In order to use it, you must import it and main.

Using Event

    import __main__

    from appengine_utilities import event

Subscribing to an Event

    AEU_Events.subscribe("myEventFired", self.myCallback, {"msg": "myEventFired was fired, and myCallback was launched."})

      def myCallback(self, msg):
        print msg

AEU_Events is set when you import main and event. The above code uses it's subscribe method to have myCallback run whenever something fires "myEventFired", with the argument msg.

Firing an Event

    AEU_Events.fire_event("myEventFired")

When fire_event is used, all callbacks subscribed to myEventFired will then run, along with any additional arguments passed as part of the subscribe call.

Order is important

Event subscriptions must happen before the event is fired. Callbacks subscribed after an event is fired will not be run.

Events included in other utilities

Sessions and Cache both already include fire events if you import Event as well as them. The list is:

  • Sessions:
    • sessionInititalized: Fires after session intializtion.
    • sessionGetFromDatastore: Fires after a session retrieved from the datastore.
    • sessionDataGetFromDatastore: Fires after session data is retrieved from the datastore.
    • sessionDataGetFromMemcache: Fires after session data is retrieved from memcache.
    • sessionDataPut: Fires after session data is added.
    • sessionMemcacheUpdated: Fired every time memcache is updated. This happens anytime new session data is added and when data is retrieved from the datastore because it could not be found in memcache.
  • Cache:
    • cacheInitialized: Fired after cache is initalized.
    • cacheAdded: Fired after a cache object is added.
    • cacheSet: Fired after a cache object is set.
    • cacheReadFromDatastore: Fired after a cache object is read from the datastore.
    • cacheReadFromMemcache: Fired after a cache object is read from memcache.
    • cacheRead: Fired after a cache object is read (regardless of source)
    • cacheDeleted: Fired after a cache object is deleted.

Comment by gramki, Sep 19, 2009

Isn't this strange to have arguments in subscribe and to have in fire_event? Usually the callback would be interested in the state/additional info about the event when fired, not when subscribed right?

Comment by bowman.joseph, Sep 19, 2009

fire_event is basically a hook, I guess that might be another way of thinking of the event system. Coming from a primarily javascript background at the time I wrote this, I perhaps should have called this hooks, instead of event.

What you do is you place fire_event in your application where you might want to hook into later. When the event is fired it looks for callbacks subscribed to it, and runs them. This is why the arguments exist on subscribe, fire_event is nothing more than the utility that launches them

Comment by bowman.joseph, Sep 19, 2009

As I'm working towards the next release, I'll try to improve the docstring to spell this out better, which will also replace the content of this page when I'm done.


Sign in to add a comment
Hosted by Google Code