The PEAK Developers' Center   TwistedDeferredForPeakEvents UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View

NOTE: This code has been incorporated into peak.events and has evolved significantly since then. This page should go away.

    1 from __future__ import generators
    2 from peak.api import *
    3 from twisted.internet import defer
    4 from twisted.python import failure
    5 
    6 import traceback
    7 
    8 class DeferredAsEventSource(protocols.Adapter):
    9 
   10     protocols.advise(
   11         instancesProvide=[events.ITaskSwitch],
   12         asAdapterForTypes=[defer.Deferred],
   13     )
   14 
   15     def nextAction(self, thread=None, state=None):
   16 
   17         if state is not None:
   18 
   19             def handler():
   20                 v = events.resume()
   21                 if isinstance(v,failure.Failure):
   22                     raise v
   23                 else:
   24                     yield v
   25 
   26             state.CALL(handler())
   27 
   28             if self.subject.called:
   29                 state.YIELD(self.subject.result)
   30             else:
   31                 cb = lambda v: (thread.step(self,v),v)[1]
   32                 self.subject.addCallbacks(cb,cb)
   33 
   34         return self.subject.called
   35 
   36 
   37 def testDeferred(d):
   38     try:
   39         yield d; print events.resume()
   40     except:
   41         traceback.print_exc()
   42 
   43 d = defer.Deferred()
   44 t = events.Thread(testDeferred(d))
   45 d.callback(42)
   46 t = events.Thread(testDeferred(d))
   47 
   48 d = defer.Deferred()
   49 t = events.Thread(testDeferred(d))
   50 d.errback(ValueError)
   51 t = events.Thread(testDeferred(d))

PythonPowered
EditText of this page (last modified 2004-07-12 22:16:21)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck