[Twisted-Python] Re : executing .rpy in a thread

Jerome Jushima ccom at gmx.net
Tue Sep 16 14:38:14 EDT 2003


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.

I am also not coherent in my head of how to pass the request variable to my
doLongPageGeneration function.

Is it correct how I  registered the callbacks?

Thanks to all the twisted gurus,  _stephan

---------------------------------------------------start

 from twisted.web import server, resource, static, script
 from twisted.internet import reactor
 from twisted.internet import threads

def doLongPageGeneration():
     return "<html><b>H</b>ello, <b>W</b>orld!</html>"

class Simple(resource.Resource):
     isLeaf = True
     def render(self, request):
         d = threads.deferToThread(doLongPageGeneration)
         d.addCallback(request.write)
         d.addCallback(request.finish)
         return "NOT_DONE_YET"


site = server.Site(Simple())
reactor.listenTCP(8080, site)

reactor.run()

------------------------------------------------------end

> The easiest way to run code in a thread and get the result back is with 
> twisted.internet.threads.deferToThread.  Your Resource's render method 
> would need to return NOT_DONE_YET, and you'd need to add callbacks to 
> the Deferred from deferToThread that call request.write(...) and 
> request.finish().

>> Say I want to execute blocking code in a .rpy file. Can't I just 
>> execute all the code in a separate thread? I've found the following 
>> code to execute a function in a thread. How could I add a callback to 
>> that thread so I could get back the whole html page back from that 
>> "notthreadSafe" function and return it to the client
>> (web browser)?


-- 
+++ GMX - die erste Adresse für Mail, Message, More! +++

Getestet von Stiftung Warentest: GMX FreeMail (GUT), GMX ProMail (GUT)
(Heft 9/03 - 23 e-mail-Tarife: 6 gut, 12 befriedigend, 5 ausreichend)

Jetzt selbst kostenlos testen: http://www.gmx.net





More information about the Twisted-Python mailing list