Python Distributed RPC Framework ZERORPC installation and use tutorial __python

Source: Internet
Author: User
Tags function definition getmessage readfile stdin user definition uuid
1. Introduction and Installation

RPC makes it a lot simpler to build distributed system, and it is widely used in the implementation of cloud computing.
RPC can be asynchronous

Python implements RPC, you can use the Simplexmlrpcserver in the standard library, and the other zerorpc is the Third-party library that supports RPC
ZERORPC is based on ZeroMQ and Messagepack, the speed is relatively fast, response time is short, concurrent high

ZERORPC requires additional installation, and Simplexmlrpcserver does not require additional installation, but Simplexmlrpcserver performance is relatively poor

ZERORPC Installation:

Yum-y Install gcc*
Yum-y install gcc-c++ libuuid-devel python-uuid UUID
Yum-y Install zlib Zlib-dev

Install PIP:
wget https://bootstrap.pypa.io/get-pip.py
Python get-pip.py

PIP Installation:
Pip Install Zerorpc

ZERORPC will actually rely on Msgpack-python, PYZMQ, Future, Greenlet, gevent

Where Gevent supports the co-process

Installing collected Packages:msgpack-python, PYZMQ, Future, Greenlet, Gevent, Zerorpc Running, setup.py install for
  m Sgpack-python ... do
  Running setup.py install for future ... do Running setup.py for
  install ... zerorpc C3/>successfully installed future-0.16.0 gevent-1.2.1 greenlet-0.4.12 msgpack-python-0.4.8 pyzmq-16.0.2 zerorpc-0.6.1

Official website reference: http://www.zerorpc.io/ 2. Using the example:

Description: This experiment ran successfully in the CENTOS6 version,
But in Centos7 debugging has always been abnormal, performance, ZERORPC request Timeout zerorpc.exceptions.LostRemote:Lost remote after 10s heartbeat
This error may also occur when the request does not have a corresponding response function, ZERORPC will not throw an exception, wait for timeout

Traceback (most recent): File "<stdin>", line 1, in <module> File "/usr/local/python27/lib/pytho n2.7/site-packages/zerorpc/core.py ", line 278, in <lambda> return Lambda *args, **kargs:self (method, *args, **k args) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/core.py", line 270, at __call__ return Self._pro Cess_response (request_event, Bufchan, timeout) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/ core.py ", line the, in _process_response reply_event = Bufchan.recv (timeout=timeout) File"/usr/local/python27/lib/p ython2.7/site-packages/zerorpc/channel.py ", line 255, in recv event = Self._input_queue.get (timeout=timeout) File"/ usr/local/python27/lib/python2.7/site-packages/gevent/queue.py ", line 284, in Get return Self.__get_or_peek (self._ Get, blocks, timeout) File "/usr/local/python27/lib/python2.7/site-packages/gevent/queue.py", line 261, in __get_or_ Peek result = Waiter.get () File "/usr/local/python27/lib/python2.7/site-packages/gevent/hub.py ", line 899, in Get Return Self.hub.switch () File"/usr/loca l/python27/lib/python2.7/site-packages/gevent/hub.py ", line 630, in Switch return Rawgreenlet.switch (self) zerorpc.ex Ceptions. Lostremote:lost Remote after 10s heartbeat

1. Basic Application

Service side:

Import Zerorpc

class Hellorpc (object):
    def hello (self, name): Return
        "Hello,%s"% name

s = zerorpc. Server (Hellorpc ())
s.bind ("tcp://0.0.0.0:4242")
S.run ()

Client:

Import Zerorpc

c = zerorpc. Client ()
c.connect ("tcp://127.0.0.1:4242")
print C.hello ("RPC")

Results:

>>> c.connect ("tcp://127.0.0.1:4242")
[None]
>>> print C.hello ("RPC")
Hello, RPC
>>> 

Note:
Service-side s = zerorpc. Server (Hellorpc ()), service registration, registration of only one class, if registered multiple, then the runtime will not normally have an exception, but the customer service to send call request, execution may throw an exception
For example:

Server:

import Zerorpc

class Hellorpc (object):
    def hello (self, name): Return
        "Hello,%s"% Name
class Resrpc (object):
    def x (self, name): Return
        "Hello,%s"% name

s = zerorpc. Server (Hellorpc (), Resrpc ())
#或注册两个
#s = Zerorpc. Server (Resrpc ())
#s = Zerorpc. Server (Hellorpc ())
s.bind ("tcp://0.0.0.0:4242")
S.run ()

client:
import zerorpc

c = zerorpc. Client ()
c.connect ("tcp://127.0.0.1:4242")
print C.hello ("rpc")
print c.x ("RPC")

When the Execute print c.x ("RPC") throws an exception similar to the following: Raise Nameerror (Event.name)

Traceback (most recent):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python27/lib /python2.7/site-packages/zerorpc/core.py ", line 278, in <lambda> return
    Lambda *args, **kargs:self (method, * args, **kargs)
  File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/core.py", line 270, in __call__ Return
    self._process_response (request_event, Bufchan, timeout)
  File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/core.py", line 238, _process_response
    reply_ event, Self._handle_remote_error)
  File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/patterns.py" , line, in Process_answer
    raise exception
Zerorpc.exceptions.RemoteError:Traceback (most recent call last): C10/>file "/usr/local/python27/lib/python2.7/site-packages/zerorpc/core.py", line 152, in _async_task
    raise Nameerror (event.name)
Nameerror:listinfo

2. Streaming response

Import Zerorpc

class Streamingrpc (object):
    @zerorpc. Stream
    def streaming_range (Self, FR, to, step):
        Return Xrange (FR, to, step)

s = zerorpc. Server (Streamingrpc ())
s.bind ("tcp://0.0.0.0:4242")
S.run ()

Client:

Import Zerorpc

c = zerorpc. Client ()
c.connect ("tcp://127.0.0.1:4242") for

item in C.streaming_range (2):
    Print Item

Results:

>>> c.connect ("tcp://127.0.0.1:4242")
[None]
>>> 
>>> for item in C.streaming_range (2):
...    Print Item
... 
of
>>>     

Note:
@zerorpc. Stream the function modification here is necessary, otherwise there will be anomalies, such as Typeerror:can ' t serialize

Import Zerorpc

class Streamingrpc (object):
    def streaming_range (Self, FR, to, step): Return
        xrange (FR, To, Step)

s = zerorpc. Server (Streamingrpc ())
s.bind ("tcp://0.0.0.0:4242")
S.run ()

Exception information:

    Raise Exception Zerorpc.exceptions.RemoteError:Traceback (most recent call last): File "/usr/local/python27/lib/pyt hon2.7/site-packages/zerorpc/core.py ", line 153, in _async_task Functor.pattern.process_call (Self._context, Bufchan,
    event, functor) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/patterns.py", line, in Process_call Channel.emit_event (rep_event) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/channel.py", line 234, In Emit_event self._channel.emit_event (event) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/ heartbeat.py ", line 116, in Emit_event self._channel.emit_event (event, timeout) File"/usr/local/python27/lib/python 2.7/site-packages/zerorpc/channel.py ", line 154, in Emit_event self._multiplexer.emit_event (event, timeout) File"/u sr/local/python27/lib/python2.7/site-packages/zerorpc/channel.py ", line $, in Emit_event return self._events.emit_ Event (event, timeout) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/events.py ", line 347, in Emit_event parts.extend ([b ', Event.pack ()]) File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/events.py", line A, in pack R = Msgpack. Packer (Use_bin_type=true). Pack (payload) File "Msgpack/_packer.pyx", line 231, in Msgpack._packer. Packer.pack (msgpack/_packer.cpp:3661) File "Msgpack/_packer.pyx", line 233, in Msgpack._packer. Packer.pack (msgpack/_packer.cpp:3503) File "Msgpack/_packer.pyx", line 221, in Msgpack._packer. Packer._pack (msgpack/_packer.cpp:3230) File "Msgpack/_packer.pyx", line 221, in Msgpack._packer. Packer._pack (msgpack/_packer.cpp:3230) File "Msgpack/_packer.pyx", line 228, in Msgpack._packer. Packer._pack (msgpack/_packer.cpp:3382) Typeerror:can ' t serialize xrange (10, 20, 2)

3. Pass in multiple parameters

Service side:

Import Zerorpc

class Myrpc (object):
    def listinfo (self,message): Return
        "Get Info:%s"%message

    def Getpow (self,n,m): Return
        n**m           

s = zerorpc. Server (Myrpc ())
s.bind ("tcp://0.0.0.0:4242")
S.run ()

Client:

Import Zerorpc

c = zerorpc. Client ()
c.connect ("tcp://127.0.0.1:4242")
print C.listinfo ("This is Test string")
print C.getpow ( 2,5)

Note: In the function definition, you need to define a default self, otherwise the TypeError exception will be thrown:
Typeerror:getpow () takes exactly 2 arguments (3 given)
For example:
Change the Getpow to read as follows:

    def getpow (n,m): Return
        n**m     

Exception information:

    Raise Exception
Zerorpc.exceptions.RemoteError:Traceback (most recent call last):
  File "/usr/local/python27/ lib/python2.7/site-packages/zerorpc/core.py ", line 153, in _async_task
    Functor.pattern.process_call (self._ Context, Bufchan, event, functor)
  File "/usr/local/python27/lib/python2.7/site-packages/zerorpc/patterns.py", Line at Process_call result
    = functor (*req_event.args)
  File "/usr/local/python27/lib/python2.7/ site-packages/zerorpc/decorators.py ", line, in __call__ return
    self._functor (*args, **kargs)
TypeError: Getpow () takes exactly 2 arguments (3 given)

4. Acquisition Time
Service side

Import zerorpc
import datetime

class GetMessage (object):
    def showget (self, str): Return ' Get message
        : %s '%str
    def Showtime (self): now        
         = Datetime.datetime.now () return
         now.strftime ('%y-%m-%d%h:%m:%s ')  

s = zerorpc. Server (GetMessage ())
s.bind ("tcp://0.0.0.0:8081")
S.run ()

Customer Service End:

Import zerorpc

myclient = Zerorpc. Client ()
myclient.connect ("tcp://192.168.137.19:8081")
print Myclient.showget ("RPC")
print Myclient.showtime ()

5. Return the contents of the file

Service side:

Import Zerorpc

class ReadFile (object):
    def readone (self,filepath):
        try:
          f = open (filepath)
          line = F.readline () return
          '%s file list one:%s '% (filepath,line)
        except: Return
          ' could not open Filepath,file Ma Y not exists. "  

    def readall (Self,filepath):        
        try:
          f = open (filepath)
          lines = F.readlines ()
          strfile=[]
          for Line in lines:
              strfile.append [line] return
          '%s file list one:%s '% (filepath,strfile)
        except:
          Return ' could not open filepath,file may not exists. '  

s = zerorpc. Server (ReadFile ())
s.bind ("tcp://0.0.0.0:8081")
S.run ()

Client:

Import zerorpc

myclient = Zerorpc. Client ()
myclient.connect ("tcp://192.168.137.19:8081")
print Myclient.readone ("Ip.txt")
print Myclient.readall ()

Results:

>>> print Myclient.readone ("Ip.txt")
ip.txt file list one:192.168.1.1

>>> print Myclient.readall ("Ip.txt")
ip.txt file list one: [' 192.168.1.1\n ', ' 192.168.1.2\n ', ' 192.168.1.3\n ', ' 192.168.1.4 ' \ n ', ' 192.168.1.5\n ', ' 192.168.1.6\n ']

Other:
For variable parameter **kwargs, incoming results are temporarily unsuccessful, so it is not recommended to use variable parameters

This could be due to the multiple variable parameters defined within the ZERORPC implementation, so it is not possible to pass in the incoming user definition
For example: Self._functor (*args, **kargs)
Cases:

Server:
import Zerorpc

class Myrpc (object):
    def func (Self,**kwargs):
       mes=[] for
       key in Kwargs:
           ks= '%s:%s '% (key, Kwargs[key])
           Mes.append (KS) return

       mes     

s = zerorpc. Server (Myrpc ())
s.bind ("tcp://0.0.0.0:4242")
S.run ()



client:
import zerorpc

c = zerorpc. Client ()
c.connect ("tcp://127.0.0.1:4242")
print C.func (a= "abc")

The desired result is not returned:

>>> c.connect ("tcp://127.0.0.1:4242") [None] >>> print c.func (a= "abc") [] 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.