Twisted PB: returning result as soon as ready

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Sat Sep 26 16:44:47 CEST 2009


On 25 Sep, 01:25 pm, jacopo.pecci at gmail.com wrote:
>In the following chunk of code the CLIENT receives both the results
>from  1Ccompute 1D at the same time (i.e. when the second one has
>finished). This way it cannot start  1CelaborateResult 1D on the first
>result while the SERVER is still running the second  1Ccompute 1D.
>How could I get the first result as soon as ready and therefore
>parallelize things?
>thanks, Jacopo
>
>SERVER:
>
>fibo=Fibonacci()
>fact=pb.PBServerFactory(fibo)
>reactor.listenTCP(port,  fact)
>reactor.run()
>
>CLIENT:
>
>fact=pb.PBClientFactory()
>reactor.connectTCP(host, port,   fact)
>d=fact.getRootObject()
>n1=10000
>d.addCallback(lambda obj: obj.callRemote("compute", n1))
>d.addCallback(elaborateResult)
>
>d2=fact.getRootObject()
>n2=10000
>d2.addCallback(lambda obj: obj.callRemote("compute",  n2))
>d2.addCallback(elaborateResult)
>
>reactor.run()

"elaborateResult" will be called the first time as soon as the Deferred 
returned by the first "compute" call fires.  This will happen as soon as 
the client receives the response from the server.

If you're seeing the first call of "elaborateResult" not happen until 
after the server has responded to the second "compute" call's Deferred 
fires, then it's probably because the two Deferreds are firing at almost 
exactly the same time, which means the server is returning the results 
at almost exactly the same time.

Considering your questions in another thread, my suspicion is that your 
Fibonacci calculator is blocking the reactor with its operation, and so 
even though it finishes doing the first calculation long before it 
finishes the second, it cannot actually *send* the result of the first 
calculation because the second calculation blocks it from doing so. 
Once the second calculation completes, nothing is blocking the reactor 
and both results are sent to the client.

Jean-Paul



More information about the Python-list mailing list