mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.5 KiB
96 lines
3.5 KiB
6 years ago
|
"""
|
||
|
Author: Zhongying Wang
|
||
|
Email: kerbalwzy@gmail.com
|
||
|
DateTime: 2019-08-13T23:30:00Z
|
||
|
PythonVersion: Python3.6.3
|
||
|
"""
|
||
6 years ago
|
import os
|
||
|
import sys
|
||
6 years ago
|
import time
|
||
|
import grpc
|
||
6 years ago
|
|
||
6 years ago
|
from threading import Thread
|
||
|
from concurrent import futures
|
||
|
|
||
6 years ago
|
# add the `demo_grpc_dps` dir into python package search paths
|
||
|
BaseDir = os.path.dirname(os.path.abspath(__file__))
|
||
|
sys.path.insert(0, os.path.join(BaseDir, "demo_grpc_pbs"))
|
||
|
|
||
|
from demo_grpc_pbs import demo_pb2, demo_pb2_grpc
|
||
|
|
||
6 years ago
|
SERVER_ADDRESS = 'localhost:23334'
|
||
|
SERVER_ID = 1
|
||
6 years ago
|
|
||
|
|
||
|
class DemoServer(demo_pb2_grpc.GRPCDemoServicer):
|
||
|
|
||
|
# 简单模式
|
||
6 years ago
|
# unary-unary
|
||
6 years ago
|
def SimpleMethod(self, request, context):
|
||
6 years ago
|
print("SimpleMethod called by client(%d) the message: %s" % (request.client_id, request.request_data))
|
||
|
response = demo_pb2.Response(server_id=SERVER_ID, response_data="Python server SimpleMethod Ok!!!!")
|
||
|
return response
|
||
6 years ago
|
|
||
|
# 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应)
|
||
6 years ago
|
# stream-unary (In a single call, the client can transfer data to the server several times,
|
||
6 years ago
|
# but the server can only return a response once.)
|
||
6 years ago
|
def ClientStreamingMethod(self, request_iterator, context):
|
||
|
print("ClientStreamingMethod called by client...")
|
||
6 years ago
|
for request in request_iterator:
|
||
|
print("recv from client(%d), message= %s" % (request.client_id, request.request_data))
|
||
|
response = demo_pb2.Response(server_id=SERVER_ID, response_data="Python server ClientStreamingMethod ok")
|
||
|
return response
|
||
6 years ago
|
|
||
|
# 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应)
|
||
6 years ago
|
# unary-stream (In a single call, the client can only transmit data to the server at one time,
|
||
6 years ago
|
# but the server can return the response many times.)
|
||
6 years ago
|
def ServerStreamingMethod(self, request, context):
|
||
6 years ago
|
print("ServerStreamingMethod called by client(%d), message= %s" % (request.client_id, request.request_data))
|
||
6 years ago
|
|
||
|
# 创建一个生成器
|
||
6 years ago
|
# create a generator
|
||
|
def response_messages():
|
||
6 years ago
|
for i in range(5):
|
||
6 years ago
|
response = demo_pb2.Response(server_id=SERVER_ID, response_data=("send by Python server, message=%d" % i))
|
||
|
yield response
|
||
6 years ago
|
|
||
6 years ago
|
return response_messages()
|
||
6 years ago
|
|
||
|
# 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据)
|
||
6 years ago
|
# stream-stream (In a single call, both client and server can send and receive data
|
||
6 years ago
|
# to each other multiple times.)
|
||
6 years ago
|
def BidirectionalStreamingMethod(self, request_iterator, context):
|
||
|
print("BidirectionalStreamingMethod called by client...")
|
||
|
|
||
6 years ago
|
# 开启一个子线程去接收数据
|
||
|
# Open a sub thread to receive data
|
||
6 years ago
|
def parse_request():
|
||
|
for request in request_iterator:
|
||
|
print("recv from client(%d), message= %s" % (request.client_id, request.request_data))
|
||
6 years ago
|
|
||
6 years ago
|
t = Thread(target=parse_request)
|
||
6 years ago
|
t.start()
|
||
|
|
||
|
for i in range(5):
|
||
6 years ago
|
yield demo_pb2.Response(server_id=SERVER_ID, response_data=("send by Python server, message= %d" % i))
|
||
6 years ago
|
|
||
|
t.join()
|
||
|
|
||
|
|
||
|
def main():
|
||
|
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
||
|
|
||
|
demo_pb2_grpc.add_GRPCDemoServicer_to_server(DemoServer(), server)
|
||
|
|
||
6 years ago
|
server.add_insecure_port(SERVER_ADDRESS)
|
||
6 years ago
|
print("------------------start Python GRPC server")
|
||
|
server.start()
|
||
|
|
||
|
# In python3, `server` have no attribute `wait_for_termination`
|
||
|
while 1:
|
||
|
time.sleep(10)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|