def blockOn(d): """ Wait for a Deferred to fire, and return its result directly. This function must be called from a non-reactor greenlet. @return: The result of the Deferred. @raise: The exception that the Deferred was fired with. """ current = greenlet.getcurrent() synchronous = [] def cb(result): if greenlet.getcurrent() is current: synchronous.append(result) else: current.switch(result) def eb(failure): if greenlet.getcurrent() is current: synchronous.append(failure) else: failure.throwExceptionIntoGenerator(current) d.addCallbacks(cb, eb) if synchronous: if isinstance(synchronous[0], Failure): synchronous[0].raiseException() return synchronous[0] return MAIN.switch()
def wait(seconds, clock=None): """ Wait a number of seconds before returning control to the calling greenlet. @param seconds: Number of seconds to wait. @type seconds: C{int} @param clock: The clock with which to schedule the return to this greenlet. @type clock: L{twisted.internet.interfaces.IReactorTime} provider. """ if clock is None: from twisted.internet import reactor as clock thisGreenlet = greenlet.getcurrent() clock.callLater(seconds, thisGreenlet.switch) return MAIN.switch()