[Twisted-Python] executing .rpy in a thread
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)? from twisted.internet import reactor from twisted.python import threadable threadable.init(1) def notThreadSafe(x): """do something that isn't thread-safe""" # ... def threadSafeScheduler(): """Run in thread-safe manner.""" reactor.callFromThread(notThreadSafe, 3) # will run 'notThreadSafe(3)' # in the event loop _stephan
On Wed, Sep 03, 2003 at 11:18:37PM -0700, stephan wrote:
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)?
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(). -Andrew.
participants (2)
-
Andrew Bennetts -
stephan