[Twisted-Python] Capture blocking calls?

Tommi Virtanen tv at twistedmatrix.com
Thu Jun 16 05:53:23 MDT 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)
    d.addCallback(moreStuff)
    ...
    # we want "x" to be result of d here, but don't know how
    moreBlockingThings(x)

d = reactor.callInThread(bigUglyThingThatBlocks)

you can say

def smallerUglyThingThatBlocks():
    ...
    someVar = value
    ...
    return someVar

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




More information about the Twisted-Python mailing list