[Twisted-Python] Non-blocking webserver - what am I doing wrong?
![](https://secure.gravatar.com/avatar/ac10fac0febc01fb17a23adc5f30b047.jpg?s=120&d=mm&r=g)
I'm trying to get a simple webserver working using twisted. It works, to some extent, serves pages and all, but it blocks until the reactor.callLater(...) is done... what am I doing wrong? Dave # CODE from twisted.web import server from twisted.web.resource import Resource from twisted.internet import reactor, defer import pprint dump = pprint.PrettyPrinter(indent=4).pformat def requestDebug(r): r.write('<pre>'+ dump(r.headers) +'</pre>') reactor.callLater(5, r.finish) class NonBlock(Resource): """ I should be a non-blocking resource that takes 5s to load. Instead, each request waits for any others to finish. """ def getChild(self, path, request): return self isLeaf=False def render_GET(self, request): d = defer.Deferred() d.addCallback(requestDebug) d.callback(request) return server.NOT_DONE_YET from twisted.application import service, internet application = service.Application("nonblock") internet.TCPServer(8080, server.Site(NonBlock()) ).setServiceParent(application)
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Mon, 25 Apr 2005 17:21:54 -0400, Dave Gray <dgray@omniti.com> wrote:
You might have more success asking on the twisted-web mailing list: http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web Jp
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Wed, 27 Apr 2005 10:30:44 -0400, Dave Gray <dgray@omniti.com> wrote:
Indeed, you have misunderstood: exarkun@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. Jp
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Mon, 25 Apr 2005 17:21:54 -0400, Dave Gray <dgray@omniti.com> wrote:
You might have more success asking on the twisted-web mailing list: http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web Jp
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Wed, 27 Apr 2005 10:30:44 -0400, Dave Gray <dgray@omniti.com> wrote:
Indeed, you have misunderstood: exarkun@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. Jp
participants (3)
-
Dave Gray
-
Jp Calderone
-
Pavel Pergamenshchik