How a simple RPC framework is refined (III)--implementing RPC calls with parameters

Source: Internet
Author: User

In the previous article, we developed a very simple RPC message format, but left two questions

    • We do not implement the corresponding encode and decode methods, and are not based on the transfer of strings that can be cross-device, but direct memory variables.
    • The request command with parameters is not supported by RPC request now. such as Add (A, b), how to describe the parameter, A/b in the RPC message.
Let me first implement the second problem, the RPC call with parameters.

In fact, there is not much difference. Since it is to take the parameter, it can only extend the original request message, add a parameter member, used to represent the parameters, the specific format is in dictionary mode, {' Arg1 ', arg1, ' arg2 ', arg2,....}. This solves the problem of multi-parameter representation.

Class Request (object):    @RPC request, which contains both the command ID and the requested content. This implementation is related to the specific RPC protocol.    @ Here is the simplification, using Python's own dictionary as the data structure of its protocol    '    def __init__ (self):        ' '        Constructor        ' = 0 #id的作用在于将Request和Response建立绑定关系. Useful when calling asynchronously        Self.command = None #sayHello        self.parameter = {}            def __str__ (self):        return '. Join (' ID: ', str (,  '    command: ', str (self.command), '    Parameter: ', str (self.parameter)))  

The RPC request for Add (A=1, b=2) is like this.

Request:id = 3, command = ' add ', parameter = {' A ': 1, ' B ': 2}

Corresponding to the client's Add method, we can write this

def add (self, A, b):        req = Request () = 3        req.command = ' Add '        req.parameter = {' A ': A, ' B ': b}        ret Urn Self.request (req)

So after receiving this RPC request from the server, how to handle the parameters? A traditional and slightly clumsy way to do this is:

    def add (self, A, b):        return a + b        def procreqeust__add (self, req):        parameter = req.parameter        A = parameter . Get (' a ')        B = Parameter.get (' B ')        return Self.add (A, B)

The disadvantage of this approach is that there is no way to be lazy. (lazy lazy), each RPC call, how to deal with, bored dead, there is no technical content of pure labor, but also test carefully, accidentally mistaken A or B name, hehe, waiting to be invited to tea bar.

At this time, the big kill device to play, scripting language is to find a good. There's always something you can't imagine. Directly on the code

def procreqeust__add (self, req):        parameter = req.parameter        return Self.add (**parameter)

The above **parameter do not understand the classmate self-degree Niang. Here's a simple explanation: The **parmater function is the same as that clumsy code, but there is a precondition that, even if the add declaration, the parameter variable name, a, B, cannot be changed. (c + + classmate may faint, even have voice has such a saying)

At this point, using this new approach, our server code is this way, on the method without parameters, the above * * is also safe to use

def procrequest (self,req):        RSP = Response () =           if Req.command = = ' SayHello ':            rsp.result = Self.sayhello (**req.parameter)        elif Req.command = = ' Add ':            Rsp.result = Self.add (**req.parameter)        else:            Raise Exception ("Unknown Command")

Over, done, and quite satisfied.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How a simple RPC framework is refined (III)--implementing RPC calls with parameters

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: 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.