Twisted PB: returning result as soon as ready

J

jacopo

In the following chunk of code the CLIENT receives both the results
from “compute” at the same time (i.e. when the second one has
finished). This way it cannot start “elaborateResult” on the first
result while the SERVER is still running the second “compute”.
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()
 
E

exarkun

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top