The PEAK Developers' Center   events UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
The following 159 words could not be found in the dictionary of 50 words (including 50 LocalSpellingWords) and are highlighted below:
About   An   Another   Broadcaster   Deferred   Distributer   Distributor   Event   Examples   Hello   Helo   In   Nine   None   Source   Tasks   The   This   To   True   Twisted   Two   Using   We   When   World   You   about   access   adapt   also   an   and   api   are   as   assign   based   bcst   be   between   bit   built   but   by   c1   c2   c3   c4   callback   callback1   callback2   called   calling   can   cancel   canceller   case   code   create   def   difference   dist   driven   each   event   events   explicitly   features   for   from   further   generates   generator   given   giving   going   great   have   hello   import   in   is   it   iterate   iteration   its   listen   listener   listeners   ll   loop   lost   many   means   module   more   natural   object   objects   of   once   one   or   order   our   output   over   peak   pretty   print   pseudothreads   reactor   reference   return   returns   scheduling   send   sequential   simple   snippet   source   sources   stops   straightforward   style   subscribe   subscribed   subscription   subscriptions   supports   system   talk   tasks   the   them   then   they   this   to   too   triggered   untwisted   up   use   very   wait   we   were   what   when   which   will   without   world   would   write   you   your  

Clear message


1 About

This module supports event sources, listeners, subscriptions, generator-based pseudothreads ("tasks"), scheduling, and I/O events. Using 'peak.events', you can write event-driven code in a more natural, sequential, "untwisted" style, but without giving up access to Twisted's many great features. 'peak.events' can use PEAK's built-in event loop, or adapt a Twisted reactor for use as an event loop. You can also use Twisted's "Deferred" objects as event sources, which means you can use them in your Tasks (pseudothreads).

2 Examples

2.1 Hello World

To use PEAK's event system it's pretty straightforward. You create an event source, you subscribe to its event and then wait for the event to be triggered. The event source we are going to use is called Broadcaster, we'll talk about it more a bit further.

    1 from peak.events.api import *
    2 
    3 source = Broadcaster ()
    4 
    5 def callback (source, event):
    6     print "Helo world!"
    7 
    8 
    9 canceller = subscribe (source, callback)
   10 
   11 source.send (None)

The canceller object is a reference to our subscription, once the reference is lost our subscription is lost too. We can cancel our subscription explicitly by calling the canceller object.

The output would be:

Hello world! 

2.2 Another Event Source

Two event sources are called Distributor and Broadcaster. An event source is what you have to subscribe to listen for the events they send.

The difference between Distributor and a Broadcaster is a very simple one. You may assign more then one listener (or callback) to an event source. When the event source generates a given event, it will iterate over its listeners in the order in which they were subscribed and send the event to each one. In the case of a Distributer, when one of its listeners returns True, the iteration stops.

    1 from peak.events.api import *
    2 
    3 source_dist = Distributor ()
    4 source_bcst = Broadcaster ()
    5 
    6 def callback1 (source, event):
    7     print "Hello from callback1"
    8     return True
    9 
   10 def callback2 (source, event):
   11     print "hello from callback2"
   12 
   13 c1 = subscribe (source_dist, callback1)
   14 c2 = subscribe (source_dist, callback2)
   15 
   16 c3 = subscribe (source_bcst, callback1)
   17 c4 = subscribe (source_bcst, callback2)
   18 
   19 source_dist.send (None)
   20 source_bcst.send (Nine)

The output of this snippet is going to be:

Hello from callback1 
Hello from callback1 
Hello from callback2 

PythonPowered
EditText of this page (last modified 2005-08-15 12:12:40)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck