[Twisted-Python] Re : executing .rpy in a thread
Bob Ippolito
bob at redivi.com
Tue Sep 16 15:20:34 EDT 2003
On Tuesday, Sep 16, 2003, at 14:38 America/New_York, Jerome Jushima
wrote:
> With the help of your hint I have come up with the following code. For
> simplicity's sake I don't use .rpys here since it shouldn't change the
> approach.
> What happens when I run this code and open the servers root in a
> webbrowser is
> that I get a page saying "NOT_DONE_YET." Also when I get rid of the
> quotation marks in the return statement it does not seem to work.
> Python just tells
> me that it does not know NOT_DONE_YET.
This is how you want to do it, there should be no surprises with this
approach (it handles failures, logs to stdout):
from twisted.web import server, resource, static, script
from twisted.internet import reactor, threads
from twisted.python import log
import sys
def doLongPageGeneration(request):
# you *could* inspect the request object if you needed to from here
# but I wouldn't *change* it from this thread!
return "<html><b>H</b>ello, <b>W</b>orld! - Your user agent is
%s</html>" % request.getHeader('user-agent')
def writeRequestAndFinish(data, request):
request.write(data)
request.finish()
class Simple(resource.Resource):
isLeaf = True
def render(self, request):
threads.deferToThread(doLongPageGeneration,
request).addCallback(writeRequestAndFinish,
request).addErrback(request.processingFailed)
return server.NOT_DONE_YET
site = server.Site(Simple())
reactor.listenTCP(8080, site)
# I'm not sure if this is strictly necessary, but it seems like a good
idea.
reactor.initThreads()
log.startLogging(sys.stdout)
reactor.run()
More information about the Twisted-Python
mailing list