| 1 |
# Copyright 2019 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
"""The example of four ways of data transmission using gRPC in Python.""" |
| 15 |
|
| 16 |
from concurrent import futures |
| 17 |
from threading import Thread |
| 18 |
|
| 19 |
import grpc |
| 20 |
|
| 21 |
import demo_pb2 |
| 22 |
import demo_pb2_grpc |
| 23 |
|
| 24 |
__all__ = 'DemoServer' |
| 25 |
SERVER_ADDRESS = 'localhost:23333' |
| 26 |
SERVER_ID = 1 |
| 27 |
|
| 28 |
|
| 29 |
class DemoServer(demo_pb2_grpc.GRPCDemoServicer): |
| 30 |
|
| 31 |
# 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应) |
| 32 |
# unary-unary(In a single call, the client can only send request once, and the server can |
| 33 |
# only respond once.) |
| 34 |
def SimpleMethod(self, request, context): |
| 35 |
print("SimpleMethod called by client(%d) the message: %s" % |
| 36 |
(request.client_id, request.request_data)) |
| 37 |
response = demo_pb2.Response( |
| 38 |
server_id=SERVER_ID, |
| 39 |
response_data="Python server SimpleMethod Ok!!!!") |
| 40 |
return response |
| 41 |
|
| 42 |
# 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应) |
| 43 |
# stream-unary (In a single call, the client can transfer data to the server several times, |
| 44 |
# but the server can only return a response once.) |
| 45 |
def ClientStreamingMethod(self, request_iterator, context): |
| 46 |
print("ClientStreamingMethod called by client...") |
| 47 |
for request in request_iterator: |
| 48 |
print("recv from client(%d), message= %s" % |
| 49 |
(request.client_id, request.request_data)) |
| 50 |
response = demo_pb2.Response( |
| 51 |
server_id=SERVER_ID, |
| 52 |
response_data="Python server ClientStreamingMethod ok") |
| 53 |
return response |
| 54 |
|
| 55 |
# 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应) |
| 56 |
# unary-stream (In a single call, the client can only transmit data to the server at one time, |
| 57 |
# but the server can return the response many times.) |
| 58 |
def ServerStreamingMethod(self, request, context): |
| 59 |
print("ServerStreamingMethod called by client(%d), message= %s" % |
| 60 |
(request.client_id, request.request_data)) |
| 61 |
|
| 62 |
# 创建一个生成器 |
| 63 |
# create a generator |
| 64 |
def response_messages(): |
| 65 |
for i in range(5): |
| 66 |
response = demo_pb2.Response( |
| 67 |
server_id=SERVER_ID, |
| 68 |
response_data=("send by Python server, message=%d" % i)) |
| 69 |
yield response |
| 70 |
|
| 71 |
return response_messages() |
| 72 |
|
| 73 |
# 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据) |
| 74 |
# stream-stream (In a single call, both client and server can send and receive data |
| 75 |
# to each other multiple times.) |
| 76 |
def BidirectionalStreamingMethod(self, request_iterator, context): |
| 77 |
print("BidirectionalStreamingMethod called by client...") |
| 78 |
|
| 79 |
# 开启一个子线程去接收数据 |
| 80 |
# Open a sub thread to receive data |
| 81 |
def parse_request(): |
| 82 |
for request in request_iterator: |
| 83 |
print("recv from client(%d), message= %s" % |
| 84 |
(request.client_id, request.request_data)) |
| 85 |
|
| 86 |
t = Thread(target=parse_request) |
| 87 |
t.start() |
| 88 |
|
| 89 |
for i in range(5): |
| 90 |
yield demo_pb2.Response( |
| 91 |
server_id=SERVER_ID, |
| 92 |
response_data=("send by Python server, message= %d" % i)) |
| 93 |
|
| 94 |
t.join() |
| 95 |
|
| 96 |
|
| 97 |
def main(): |
| 98 |
server = grpc.server(futures.ThreadPoolExecutor()) |
| 99 |
|
| 100 |
demo_pb2_grpc.add_GRPCDemoServicer_to_server(DemoServer(), server) |
| 101 |
|
| 102 |
server.add_insecure_port(SERVER_ADDRESS) |
| 103 |
print("------------------start Python GRPC server") |
| 104 |
server.start() |
| 105 |
server.wait_for_termination() |
| 106 |
|
| 107 |
# If raise Error: |
| 108 |
# AttributeError: '_Server' object has no attribute 'wait_for_termination' |
| 109 |
# You can use the following code instead: |
| 110 |
# import time |
| 111 |
# while 1: |
| 112 |
# time.sleep(10) |
| 113 |
|
| 114 |
|
| 115 |
if __name__ == '__main__': |
| 116 |
main() |