RPC

This example walks you through a Remote Procedure Call (RPC) example

[1]:
import kiwipy
comm = kiwipy.connect('amqp://127.0.0.1')

Let’s define a couple of ‘remote’ procedures that can be we then expose to our communicator:

[2]:
def fib(comm, num):
    if num == 0:
        return 0
    if num == 1:
        return 1
    return fib(comm, num - 1) + fib(comm, num - 2)


def fac(comm, num):
    result = 1
    if num > 1:
        result = num * fac(comm, num - 1)
    return result


comm.add_rpc_subscriber(fib, 'fib')
comm.add_rpc_subscriber(fac, 'fac')
[2]:
'fac'

Now, let’s call these using their identifier

[3]:
fib_future = comm.rpc_send('fib', 30)
fib_future.result()
[3]:
832040

Cool, let’s have a look at fac then!

Ok, if you insist.

[4]:
comm.rpc_send('fac', 3).result()
[4]:
6

Nice. So, what’s the score, this looks a lot liketasks?

Yup, it’s pretty similar but the difference is in the use case. RPC calls have one unique recipient, for example imagine calling a class method remotely using an RPC to affect it’s state. Tasks on the other hand can have zero or more identical workers that process the incoming tasks.

Got it? Good.

[5]:
comm.close()