zoukankan      html  css  js  c++  java
  • zerorpc

    zerorpc

    注:python库自带的rpc模块是SimpleXMLRPCServer

    1)安装zerorpc

    #1) 通过pip
    pip3 install zerorpc 
    # ERROR:
    from _ctypes import Union, Structure, Array
    ModuleNotFoundError: No module named '_ctypes'
    # Python3中有个内置模块叫ctypes,
    # 它是Python3的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用Linux系统下的共享库(Shared library),
    # 此模块需要使用CentOS7系统中外部函数库(Foreign function library)的开发链接库(头文件和链接库)。
    # 由于在CentOS7系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了"ModuleNotFoundError: No module named '_ctypes'"的错误。
    # Methods:
    yum install libffi-devel -y 
    # 再去到python源码包安装目录重新编译安装
    make install
    

    2) 测试案例

    # server
    import zerorpc
    class FirstRpc(object):
          def first(self, content):
    	return "Hello %s" % coontent
    s = zerorpc.Server(FirstRpc())
    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.first("Nice to meet you, RPC"))
    
    

    3) 同时注册多个

    # server
    #!/usr/bin/python3
    
    
    import zerorpc
    
    _method = {}
    
    
    def register(cls):
        obj = cls()
    
        prefix = cls.__name__
        
        for k in dir(obj):
            if not k.startswith('_') and callable(getattr(obj, k)):
                x = {'{}:{}'.format(prefix, k) : getattr(obj, k)}
                _method.update(x)
        return cls
    
    #{'{}:{}'.format(prefix, k) : getattr(obj, k) for k in dir(obj) if not k.startswith('_') and callable(getattr(obj, K))}
    
    @register
    class RpcServer1(object):
        def add(self, x, y):
            return x + y
        
        def reduce(self, x, y):
            return x - y
    
    
    @register
    class RpcServer2(object):
        def add(self, x, y):
            return x * y
        
        def reduce(self, x , y):
            return x / y
    
    print("_method: %s" % _method)
    server = zerorpc.Server(_method, heartbeat=20)
    server.bind("tcp://{}:{}".format('127.0.0.1', 4242))
    server.run()
    
    """
    _method: {'RpcServer1:add': <bound method RpcServer1.add of <__main__.RpcServer1 object at 0x7f79935fca30>>, 'RpcServer1:reduce': <bound method RpcServer1.reduce of <__main__.RpcServer1 object at 0x7f79935fca30>>, 'RpcServer2:add': <bound method RpcServer2.add of <__main__.RpcServer2 object at 0x7f799357e220>>, 'RpcServer2:reduce': <bound method RpcServer2.reduce of <__main__.RpcServer2 object at 0x7f799357e220>>}
    
    """
    
    ##---------------------------------------------------------------------------------------------------------------------##
    
    # client
    
    #!/usr/bin/python3
    
    import zerorpc
    
    
    class Client(zerorpc.Client):
        def __init__(self, *args, **kwargs):
            self._prefix = kwargs.pop("prefix", "") # 没明白为什么要pop一个""
            super(Client, self).__init__(*args, **kwargs)
    
    
    #    def set_prefix(self, prefix):
    #        self._prefix = prefix
    
        def __getattr__(self, method):
            method = ":".join([self._prefix, method]) if self._prefix else method
            return lambda *args, **kwargs: self(method, *args, **kwargs)
    
    
    if __name__ == "__main__":
    
        c1 = Client(prefix="RpcServer1")
        c1.connect('tcp://127.0.0.1:4242')
        c2 = Client(prefix="RpcServer2")
        c2.connect('tcp://127.0.0.1:4242')
    
        print(c1.add(5, 5))
        print(c2.add(5, 5))
    
    
    

    参考大神的经验,还需研究

  • 相关阅读:
    注解的作用
    962. Maximum Width Ramp
    594. Longest Harmonious Subsequence
    1042. Flower Planting With No Adjacent
    419. Battleships in a Board
    1041. Robot Bounded In Circle
    leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)
    leetcode 44. Wildcard Matching(模糊匹配)
    HEU预热赛
    780. Reaching Points
  • 原文地址:https://www.cnblogs.com/persisit/p/13713585.html
Copyright © 2011-2022 走看看