One installation environment
1 python install thrift package
pip install thrift
2 Download the win version of "thrift-0.11.0.exe"
http://thrift.apache.org/download
3 Install pychar's thrift plug-in to improve the automatic completion of writing thrift
https://plugins.jetbrains.com/plugin/7331-thrift-support
Copy the .jar file after decompressing the downloaded "intellij-thrift.zip" to the lib directory under the pycharm installation directory.
Two examples of implementing a hello
1 Define hello.thrift
2 Compile with thrift compiler (using Cygwin64 Terminal)
$ ./thrift-0.11.0.exe -r -gen py hello.thrift
This generated the py code, in "gen-py". This package can be used by server and client code written by myself later.
3 Building a python project
Use pycharm to create the project "thrift_hello" project, and then copy the software package generated in the previous step to this project directory. The project directory is as follows. Because the path of the package does not seem to support '-' in Python, it is necessary to rename "gen-py" to "gen_py".
4 Write client and server implementation code.
hello_server.py
from gen_py.hello import Hello
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket, TTransport
__HOST = '127.0.0.1'
__PORT = 9090
class hello_handle:
def helloString(self, para):
print(para)
return "result:" + para
if __name__ == '__main__':
# Create server
handler = hello_handle()
processor = Hello.Processor(handler)
# Listening port
transport = TSocket.TServerSocket(host=__HOST, port=__PORT)
# Select the transport layer
tfactory = TTransport.TBufferedTransportFactory()
# Choose transfer protocol
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# Create server
server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
server.setNumThreads(5)
print('Starting the server')
server.serve()
print('done')
hello_client.py
from gen_py.hello import Hello
from thrift.Thrift import TException
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TProtocol
from thrift.transport import TSocket, TTransport
__HOST = '127.0.0.1'
__PORT = 9090
if __name__ == '__main__':
try:
print ('Client starts ...')
transport = TSocket.TSocket(host=__HOST, port=__PORT)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hello.Client(protocol)
transport.open()
result = client.helloString("hello")
print(result)
transport.close()
print('done')
except TException as e:
print(e)
5 Run the code
1. Run hello_server first
2 The output of running hello_client:
Articles directory THRIFT RPC interface test python 1. Basic command 2. Thrift's Sever design 3. THRIFT's client design 3. Follow points between communication 4. Thrift basic grammar 1. Issues that ne...
The IDL Thrift can use the following syntax to define interfaces described. 1 basic types bool: Boolean value, true or false byte: 8-bit signed integer i16: 16-bit signed integer i32: 32-bit signed in...
Scenes Introduction to Thrift and examples of using Thrift to implement RPC in Java: In the above, I talked about using Thrift to implement remote procedure calls in Java. Implemented the method of ca...
System: centos7 The Thrift compiler will generate service interface code for the server and stubs for the client based on the selected target language. The parameters can be basic types and structures...
Official website: https://thrift.apache.org/ 1 overview We replace him into the way you are easy to understand: 2 hierarchical analysis 2.1 Transport (Transport Layer) The transport layer provides a s...
The remote process calls RPC commonly used and distributed computing, corresponding to the Python library namerpyc; The upper section is the RPYC server, and the lower section is called the RPYC call;...
Thrift's python server running mode currently only supports four types, TSimpleServer, TThreadedServer, TThreadPoolServer, TForkingServer, the following is the analysis 1.TSimpleServer Single-threaded...
In the past, the multi-process service mode was realized by the mode of restarting the process in each thread by multi-threading. I discovered today that the process pool model actually comes with it....
premise Install and configure python, I installed version 2.7 Install python-Liao Xuefeng's official website pycharm-python development tool Start 1. Install python dependencies For the development of...