Article From:https://www.cnblogs.com/navysummer/p/9122661.html
  • sleep.thriftFile (what is a thrift file?) The file content is as follows, which defines a Sleep service that provides a sleep method, the sleep method accepts a 32 bit int type parameter and does not return a value
service Sleep {
    // onewayIndicates that the method has no return value, and the return value type must be void// After the client is invoked, it does not wait for the server to respond, and returns directly.// The server receives the request and executes the relevant business logic without returning any return value.Oneway void sleep (1: i32 seconds)
}
  • sleep_server.pyThe contents of the file are as follows
# -*- coding: utf-8 -*-
import time
import thriftpy
from thriftpy.rpc import make_server

# According to the sleep.thrift file, dynamically build a module named "sleep_thriftpy" in memory.
# The module contains a object named Sleep
sleep_thrift = thriftpy.load("sleep.thrift", module_name="sleep_thrift")

# The Class name in the official code is Dispatcher
# Modification to Sleep is only for the convenience of readers and corresponding Sleep services in sleep.thrift files.
class Sleep(object):
    # This method corresponds to the oneway void sleep (1: I32 seconds) in the sleep.thrift file.
    def sleep(self, seconds):
        print("I'm going to sleep %d seconds" % seconds)
        time.sleep(seconds)
        print("Sleep over!")

def main():
    # Create a service and monitor it on the 6000 port of 127.0.0.1.
    # Bind Object in class Sleep and module sleep_thrift in the name Sleep.
    server = make_server(sleep_thrift.Sleep, Sleep(),
                         '127.0.0.1', 6000)
    print("serving...")
    # Start service
    server.serve()

if __name__ == '__main__':
    main()
  • sleep_client.pyThe contents of the file are as follows
# -*- coding: utf-8 -*-
import thriftpy
from thriftpy.rpc import make_client

# According to the sleep.thrift file, a module named "sleep_thriftpy" is dynamically generated.
# The module contains a object named Sleep
sleep_thrift = thriftpy.load("sleep.thrift", module_name="sleep_thrift")

def main():
    # Create a client that connects to the 6000 port of 127.0.0.1.
    # And bind the client and server side Sleep services.
    client = make_client(sleep_thrift.Sleep, '127.0.0.1', 6000)
    # Once the binding is completed, you can directly call the corresponding method on the server side.
    client.sleep(1)

if __name__ == '__main__':
    main()

Running result

  • Execute sleep_server.py and output the following information

serving…

The sleep_client.py is executed, and the server end output is as follows

I’m going to sleep 1 seconds 
Sleep over!

summary

  • thriftThe file describes the communication interface between the server and the client in a neutral way (the Thriftpy creation server and the client need to parse the file), allowing transparent communication between the clients and the servers using different languages.

  • Developers using Thriftpy to write server / client can not care about the details of interface calls, and focus more on the development of business functions.

  • Using Thriftpy to create a Thrift server requires only three steps to complete.

    • Call the thriftpy.load method to parse the thrift file and build the corresponding module in memory.
    • Call make_server to bind the module that is built in the previous step and the class that completes the specific business function, while completing the transport layer and protocol layer setting, generating the thrift server side
    • Invoking the server.serve method to start the server
  • Using Thriftpy to create a thrift client interface requires only three steps to complete.

    • Call the thriftpy.load method to parse the thrift file and build the corresponding module in memory.
    • According to the results of the previous analysis, we call the module built by make_client on the previous step, and complete the transport layer and protocol layer settings to generate the Thirft client.
    • According to the interface generated by the client generated by the previous step, the response interface is called.

Similar Posts:

Link of this Article: A simple example of Thriftpy

Leave a Reply

Your email address will not be published. Required fields are marked *