RPyC
It is a library for symmetrical remote procedures call and distributed computing. The library allows us to use remote objects as local objects. Here a little example.
Installing RPyC
It’s easy, just use pip to install the library.
# sudo pip3 install rpyc
The server code
We are going to develop a math service that will receive one value and will calculate the Fibonacci sequence. We’re going to create a file called MathService.py in a server in the cloud.
#!/usr/bin/env python3
import rpyc
from rpyc.utils.server import ThreadedServerclass MathService(rpyc.Service):
def on_connect(self,conn):
pass
def on_disconnect(self,conn):
pass
def exposed_fib(self,n):
seq = []
a, b = 0,1
while a < n:
seq.append(a)
a, b = b, a+b
return seqif __name__ == '__main__':
ts = ThreadedServer(MathService,port=18080)
print('Service started on port 18080')
ts.start()
After creating the MathService.py file, we have to execute the following commands.
$ chmod +x MathService.py
And execute our service.
$ ./MathService
Service started on port 18080
The service will be published through the 18080 port. You have to check that you have configured the port 18080 in your firewall.
The client
To test our Cloud Math Service, we are going to use the python interactive mode.
$ python3
Python 3.5.4 (default, Oct 9 2017, 12:07:29)
[GCC 6.4.1 20170727 (Red Hat 6.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpyc
>>> conn = rpyc.connect('<service cloud address>',18080)
>>> conn.root.fib(1000)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
>>>
Note: You have to set your own service cloud address or the IP address where the service is working. If you are working in a local way, you can use localhost.
Ok, this is all. I hope you enjoy.