• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import time
17import grpc
18
19import demo_pb2_grpc
20import demo_pb2
21
22__all__ = [
23    'simple_method', 'client_streaming_method', 'server_streaming_method',
24    'bidirectional_streaming_method'
25]
26
27SERVER_ADDRESS = "localhost:23333"
28CLIENT_ID = 1
29
30# 中文注释和英文翻译
31# Note that this example was contributed by an external user using Chinese comments.
32# In all cases, the Chinese comment text is translated to English just below it.
33
34
35# 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应)
36# unary-unary(In a single call, the client can only send request once, and the server can
37# only respond once.)
38def simple_method(stub):
39    print("--------------Call SimpleMethod Begin--------------")
40    request = demo_pb2.Request(client_id=CLIENT_ID,
41                               request_data="called by Python client")
42    response = stub.SimpleMethod(request)
43    print("resp from server(%d), the message=%s" %
44          (response.server_id, response.response_data))
45    print("--------------Call SimpleMethod Over---------------")
46
47
48# 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应)
49# stream-unary (In a single call, the client can transfer data to the server several times,
50# but the server can only return a response once.)
51def client_streaming_method(stub):
52    print("--------------Call ClientStreamingMethod Begin--------------")
53
54    # 创建一个生成器
55    # create a generator
56    def request_messages():
57        for i in range(5):
58            request = demo_pb2.Request(
59                client_id=CLIENT_ID,
60                request_data=("called by Python client, message:%d" % i))
61            yield request
62
63    response = stub.ClientStreamingMethod(request_messages())
64    print("resp from server(%d), the message=%s" %
65          (response.server_id, response.response_data))
66    print("--------------Call ClientStreamingMethod Over---------------")
67
68
69# 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应)
70# unary-stream (In a single call, the client can only transmit data to the server at one time,
71# but the server can return the response many times.)
72def server_streaming_method(stub):
73    print("--------------Call ServerStreamingMethod Begin--------------")
74    request = demo_pb2.Request(client_id=CLIENT_ID,
75                               request_data="called by Python client")
76    response_iterator = stub.ServerStreamingMethod(request)
77    for response in response_iterator:
78        print("recv from server(%d), message=%s" %
79              (response.server_id, response.response_data))
80
81    print("--------------Call ServerStreamingMethod Over---------------")
82
83
84# 双向流模式 (在一次调用中, 客户端和服务器都可以向对方多次收发数据)
85# stream-stream (In a single call, both client and server can send and receive data
86# to each other multiple times.)
87def bidirectional_streaming_method(stub):
88    print(
89        "--------------Call BidirectionalStreamingMethod Begin---------------")
90
91    # 创建一个生成器
92    # create a generator
93    def request_messages():
94        for i in range(5):
95            request = demo_pb2.Request(
96                client_id=CLIENT_ID,
97                request_data=("called by Python client, message: %d" % i))
98            yield request
99            time.sleep(1)
100
101    response_iterator = stub.BidirectionalStreamingMethod(request_messages())
102    for response in response_iterator:
103        print("recv from server(%d), message=%s" %
104              (response.server_id, response.response_data))
105
106    print("--------------Call BidirectionalStreamingMethod Over---------------")
107
108
109def main():
110    with grpc.insecure_channel(SERVER_ADDRESS) as channel:
111        stub = demo_pb2_grpc.GRPCDemoStub(channel)
112
113        simple_method(stub)
114
115        client_streaming_method(stub)
116
117        server_streaming_method(stub)
118
119        bidirectional_streaming_method(stub)
120
121
122if __name__ == '__main__':
123    main()
124