[Twisted-Python] Non-blocking webserver - what am I doing wrong?

Jp Calderone exarkun at divmod.com
Wed Apr 27 10:54:16 EDT 2005

On Wed, 27 Apr 2005 10:30:44 -0400, Dave Gray <dgray at omniti.com> wrote:
>> >         d = defer.Deferred()
>> >         d.addCallback(requestDebug)
>> >         d.callback(request)
>>Note that this code is more or less equivalent to
>>            requestDebug(request)
>Except that my version returns before the requestDebug call is done. Even 
>though I'm a Deferred n00b, I don't think I misunderstood _that_. By all 
>means, correct me if I'm wrong...

  Indeed, you have misunderstood:

    exarkun at boson:~$ python
    Python 2.4.1 (#2, Mar 30 2005, 21:51:10) 
    [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from twisted.internet import defer
    >>> def requestDebug(request):
    ...     print 'Request debug called with', repr(request)
    ...     print 'Request debug returning'
    >>> requestDebug('foo')
    Request debug called with 'foo'
    Request debug returning
    >>> d = defer.Deferred()
    >>> d.addCallback(requestDebug)
    <Deferred at -0x481fef74>
    >>> d.callback('foo')
    Request debug called with 'foo'
    Request debug returning

  Deferreds don't re-arrange processing, or make functions asynchronous, or anything like that.  They only hook a result up with one or more functions for handling the result.  The result is often only available after the callback functions have been defined and added to the Deferred, but if it is available beforehand, nothing significant really changes.

  Put another way: if a Deferred has a callback, when it is given a result, the callback is invoked immediately; if a Deferred has a result, when it is given a callback, the callback is invoked immediately.


More information about the Twisted-Python mailing list