Part of twisted.internet View Source
Maintainer: Glyph Lefkowitz
|Class||CancelledError||This error is raised by default when a
|Class||TimeoutError||This exception is deprecated. It is used only by the deprecated
|Function||maybeDeferred||Invoke a function that may or may not return a
|Function||setDebugging||Enable or disable
|Class||Deferred||This is a callback which will be put off until later.|
|Class||DebugInfo||Deferred debug helper.|
|Class||FirstError||First error to occur in a
|Function||gatherResults||Returns, via a
|Function||returnValue||Return val from a
inlineCallbacks helps you write
|Class||DeferredLock||A lock for event driven systems.|
|Class||DeferredSemaphore||A semaphore for event driven systems.|
|Class||DeferredQueue||An event driven queue.|
|Class||_ConcurrencyPrimitive||No class docstring; 1/3 methods documented|
Deferredthat has already had
This is useful when you're writing synchronous code to an asynchronous
interface: i.e., some code is calling you expecting a
result, but you don't actually need to do anything asynchronous. Just
|Parameters||result||The result to give to the Deferred's 'callback' method.|
|Returns|| (type: |
Call the given function with the given arguments. If the returned
object is a
return it. If the returned object is a
and return it. Otherwise, wrap it in
return it. If an exception is raised, convert it to a
then return it.
|Parameters||f||The callable to invoke (type: Any callable)|
|args||The arguments to pass to |
|kw||The keyword arguments to pass to |
|Returns||The result of the function call, wrapped in a |
When debugging is on, the call stacks from creation and invocation are
recorded, and added to any
AlreadyCalledErrors we raise.
Deferred, a list with the results of the given
Deferreds - in effect, a "join" of multiple deferred operations.
This differs from
in that you don't need to parse the result for success/failure.
|Parameters||consumeErrors||(keyword param) a flag, defaulting to False, indicating that failures in
any of the given |
waitForDeferredhelp you write
Deferred-using code that looks like a regular sequential function. Consider the use of
inlineCallbacksinstead, which can accomplish the same thing in a more concise manner.
@deferredGenerator def thingummy(): thing = waitForDeferred(makeSomeRequestResultingInDeferred()) yield thing thing = thing.getResult() print thing #the result! hoorj!
returns something that you should immediately yield; when your generator is
thing.getResult() will either give you the
result of the
Deferred if it
was a success, or raise an exception if it was a failure. Calling
getResult is absolutely mandatory. If you do not call
it, your program will not work.
takes one of these waitForDeferred-using generator functions and converts
it into a function that returns a
result of the
be the last value that your generator yielded unless the last value is a
instance, in which case the result will be
None. If the
function raises an unhandled exception, the
errback instead. Remember that
return result won't work; use
yield result; return in place of that.
returned from your deferred generator may also errback if your generator
raised an exception. For example:
@deferredGenerator def thingummy(): thing = waitForDeferred(makeSomeRequestResultingInDeferred()) yield thing thing = thing.getResult() if thing == 'I love Twisted': # will become the result of the Deferred yield 'TWISTED IS GREAT!' return else: # will trigger an errback raise Exception('DESTROY ALL LIFE')
Put succinctly, these functions connect deferred-using code with this
'fake blocking' style in both directions:
converts from a
the 'blocking' style, and
converts from the 'blocking' style to a
Note: this is currently implemented by raising an exception derived from
You might want to change any 'except:' clauses to an 'except Exception:'
clause so as not to catch this exception.
Also: while this function currently will work when called from within arbitrary functions called from within the generator, do not rely upon this behavior.
inlineCallbacks helps you write
code that looks like a regular sequential function. For example:
@inlineCallBacks def thingummy(): thing = yield makeSomeRequestResultingInDeferred() print(thing) # the result! hoorj!
When you call anything that results in a
can simply yield it; your generator will automatically be resumed when the
Deferred's result is available. The generator will be sent the result of
the 'send' method on generators, or if the result was a failure,
Your inlineCallbacks-enabled generator will return a
object, which will result in the return value of the generator (or will
fail with a failure object if your generator raises an unhandled
exception). Note that you can't use
return result to return a
returnValue(result) instead. Falling off the end of
the generator, or simply using
return will cause the
have a result of
result = yield result returnValue(result)
returned from your deferred generator may errback if your generator raised
@inlineCallbacks def thingummy(): thing = yield makeSomeRequestResultingInDeferred() if thing == 'I love Twisted': # will become the result of the Deferred returnValue('TWISTED IS GREAT!') else: # will trigger an errback raise Exception('DESTROY ALL LIFE')