Parallel execution of RPC procedure

#1

Hi,

I have this situation: one registered RPC procedure in backend file that for test purposes simulates some long computing (about 5 sec.) and two clients that call that procedure in onJoin and print result (source files attached). I run these clients in 2-3 sec. intervals and what happens is that first client receives result only when backend finishes processing call from second client. In other words, client1 and client2 receive result at the same time but logically client1 should get result 2-3 sec. earlier? Am I doing something wrong or does corssbar work like this?

best regards,

BL

client1.py (543 Bytes)

client2.py (544 Bytes)

hello.py (2.87 KB)

0 Likes

#2

Hi,

you are blocking the main thread reactor of the callee component by running a tight computational loop. You probably want to run this loop on a background thread - on a multi-core machine, to actually achieve parallel/concurrent computation.

Try something like this:

from twisted.internet.threads import deferToThread

class AppSession(ApplicationSession):

     log = Logger()

     @inlineCallbacks
     def onJoin(self, details):

         def test(param):
             res = 0
             for x in range(0, 50000):
                 # computing ...
                 res += param * x
             return x

         def wrap_test(param):
             return deferToThread(test, param)

         yield self.register(wrap_test, 'com.test')

Cheers,
/Tobias

PS: don't use "print" in tests like these .. this goes to console, and there are a lot of unrelated effects that can result from that.

···

Am 10.11.2016 um 22:28 schrieb Bla zej:

Hi,

I have this situation: one registered RPC procedure in backend file that
for test purposes simulates some long computing (about 5 sec.) and two
clients that call that procedure in onJoin and print result (source files
attached). I run these clients in 2-3 sec. intervals and what happens is
that first client receives result only when backend finishes processing
call from second client. In other words, client1 and client2 receive result
at the same time but logically client1 should get result 2-3 sec. earlier?
Am I doing something wrong or does corssbar work like this?

best regards,
BL

0 Likes

#3

Thank you very much for your response. So should wrapping procedures in deferToThread should be kind of pattern? In other words, should I always use deferToThread in order to achieve nonblocking procedures?

···

On Friday, November 11, 2016 at 9:33:27 AM UTC+1, Tobias Oberstein wrote:

Hi,

you are blocking the main thread reactor of the callee component by
running a tight computational loop. You probably want to run this loop
on a background thread - on a multi-core machine, to actually achieve
parallel/concurrent computation.

Try something like this:


from twisted.internet.threads import deferToThread



class AppSession(ApplicationSession):



     log = Logger()



     @inlineCallbacks

     def onJoin(self, details):



         def test(param):

             res = 0

             for x in range(0, 50000):

                 # computing ...

                 res += param * x

             return x



         def wrap_test(param):

             return deferToThread(test, param)



         yield self.register(wrap_test, 'com.test')

Cheers,

/Tobias

PS: don’t use “print” in tests like these … this goes to console, and
there are a lot of unrelated effects that can result from that.

Am 10.11.2016 um 22:28 schrieb Bla zej:

Hi,

I have this situation: one registered RPC procedure in backend file that

for test purposes simulates some long computing (about 5 sec.) and two

clients that call that procedure in onJoin and print result (source files

attached). I run these clients in 2-3 sec. intervals and what happens is

that first client receives result only when backend finishes processing

call from second client. In other words, client1 and client2 receive result

at the same time but logically client1 should get result 2-3 sec. earlier?

Am I doing something wrong or does corssbar work like this?

best regards,

BL

0 Likes