[Twisted-Python] connection pool

Tjerk.Kusters at imtech.nl Tjerk.Kusters at imtech.nl
Thu Jan 17 08:44:49 EST 2008


>will support Deferreds so this will get easier.  However, until then, you
>can still have asynchronous render_GET implementations.  You just have to
>use NOT_DONE_YET.

I have made some changes to my test code and it works. A request to
module_1 is possible will there is a request to moduleSlow2.
Multiple calls to moduleSlow2 have to wait until the other one finishes.
But calling module_1 still works. Should be oke for my webapplication.

changed it to:

from twisted.web import resource, static, server
from twisted.web.server import NOT_DONE_YET
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.threads import deferToThread
import datetime, time

def tijd():
    return datetime.datetime.today().strftime("%Y-%m-%dT%H:%M:%S")

class module_1(resource.Resource):
    def render_GET(self, request):
        request.write(tijd())
        return ""

class moduleSlow2(resource.Resource):

    def render_GET(self, request):
        d = deferToThread(self.answer, request)
        d.addCallback(request.write)
        d.addCallback(lambda _: request.finish())
        return NOT_DONE_YET

    def answer(self, request):
        time.sleep(10)
        return "finished %s" % tijd()

root = static.File("d:/www")
root.putChild("module1", module_1())
root.putChild("slow2", moduleSlow2())
reactor.listenTCP(8900, server.Site(root))
print "Reactor run"
reactor.run( )



Regars,
Tjerk





More information about the Twisted-Python mailing list