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

Dave Gray dgray at omniti.com
Mon Apr 25 15:21:54 MDT 2005


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)




More information about the Twisted-Python mailing list