[Twisted-Python] Capture blocking calls?

Tommi Virtanen tv at twistedmatrix.com
Thu Jun 16 07:53:23 EDT 2005

Luc Stepniewski wrote:
> You're right, that's what I said I was doing. I was just making the point that 
> it's easy to call methods from that thread using callFromThread(), but it's a 
> pain to call a method which returns a deferred. Hopefully there's exarkun's 

Well, you have a few possibilities.

1. I haven't checked, but I guess a function called with callInThread
   could return a deferred.

2. If you need to interleave blocking and Deferred operations, split
it out. Instead of

def bigUglyThingThatBlocks():
    someVar = value
    d = something(someVar)
    # we want "x" to be result of d here, but don't know how

d = reactor.callInThread(bigUglyThingThatBlocks)

you can say

def smallerUglyThingThatBlocks():
    someVar = value
    return someVar

d = reactor.callInThread(smallerUglyThingThatBlocks)
d.addCallback(lambda x: reactor.callInThread(moreBlockingThings, x))

More information about the Twisted-Python mailing list