|
-2
|
Twisted FAQs clearly state that "deferreds do not magically convert blocking code into non-blocking code". So, how do you magically convert blocking code into non-blocking code? This recipe is the solution!
How to make blocking code non-blocking is the obvious question for everybody using Twisted, but the Twisted documentation does not make easy to find the solution :-( The trick is to run the blocking function in a separate thread. Here
all the magic is performed by the decorator, In short: every time you have a blocking function in your code, wrap
it with the
Tags: threads
|
2 comments
Add a comment
Sign in to comment
Download
Copy to clipboard

deferToThread. Valentino Volonghi points out to me that there is already a function in Twisted doing the same job as my 'callInThread', and doing it better: twisted.internet.threads.deferToThread.
So just import it and define the decorator as 'deferred=deferToThread.__get__'
Valentino also points out that threads are not scalable, but I bet you already knew that ;)
You don't want to use this code. Callbacks you add to the Deferred will run in the thread, but only sometimes. This makes thread safety really hard, possibly impossible and means lots of obscure hard to track down bugs.
Instead, use twisted.internet.threads.deferToThread, where callbacks added to result will run in the Twisted main thread.