[Twisted-Python] Non-blocking webserver - what am I doing wrong?
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)
On Mon, 25 Apr 2005 17:21:54 -0400, Dave Gray <dgray@omniti.com> wrote:
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?
You might have more success asking on the twisted-web mailing list: http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web Jp
On Mon, 25 Apr 2005 17:21:54 -0400 Dave Gray <dgray@omniti.com> wrote:
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
...snip...
d = defer.Deferred() d.addCallback(requestDebug) d.callback(request)
...snip... Note that this code is more or less equivalent to requestDebug(request)
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...
On Wed, 27 Apr 2005 10:30:44 -0400, Dave Gray <dgray@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@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
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.
Gotcha, thanks.
participants (3)
-
Dave Gray
-
Jp Calderone
-
Pavel Pergamenshchik