Fix: reformat code with YAPF script, and check with PyLint

pull/19938/head
kerbalwzy 5 years ago
parent 25a1caa9b0
commit 5c0fed241d
  1. 29
      examples/python/data_transmission/client.py
  2. 32
      examples/python/data_transmission/server.py

@ -13,9 +13,11 @@ CLIENT_ID = 1
# only respond once.)
def simple_method(stub):
print("--------------Call SimpleMethod Begin--------------")
request = demo_pb2.Request(client_id=CLIENT_ID, request_data="called by Python client")
request = demo_pb2.Request(
client_id=CLIENT_ID, request_data="called by Python client")
response = stub.SimpleMethod(request)
print("resp from server(%d), the message=%s" % (response.server_id, response.response_data))
print("resp from server(%d), the message=%s" % (response.server_id,
response.response_data))
print("--------------Call SimpleMethod Over---------------")
@ -29,11 +31,14 @@ def client_streaming_method(stub):
# create a generator
def request_messages():
for i in range(5):
request = demo_pb2.Request(client_id=CLIENT_ID, request_data=("called by Python client, message:%d" % i))
request = demo_pb2.Request(
client_id=CLIENT_ID,
request_data=("called by Python client, message:%d" % i))
yield request
response = stub.ClientStreamingMethod(request_messages())
print("resp from server(%d), the message=%s" % (response.server_id, response.response_data))
print("resp from server(%d), the message=%s" % (response.server_id,
response.response_data))
print("--------------Call ClientStreamingMethod Over---------------")
@ -42,10 +47,12 @@ def client_streaming_method(stub):
# but the server can return the response many times.)
def server_streaming_method(stub):
print("--------------Call ServerStreamingMethod Begin--------------")
request = demo_pb2.Request(client_id=CLIENT_ID, request_data="called by Python client")
request = demo_pb2.Request(
client_id=CLIENT_ID, request_data="called by Python client")
response_iterator = stub.ServerStreamingMethod(request)
for response in response_iterator:
print("recv from server(%d), message=%s" % (response.server_id, response.response_data))
print("recv from server(%d), message=%s" % (response.server_id,
response.response_data))
print("--------------Call ServerStreamingMethod Over---------------")
@ -54,19 +61,23 @@ def server_streaming_method(stub):
# stream-stream (In a single call, both client and server can send and receive data
# to each other multiple times.)
def bidirectional_streaming_method(stub):
print("--------------Call BidirectionalStreamingMethod Begin---------------")
print(
"--------------Call BidirectionalStreamingMethod Begin---------------")
# 创建一个生成器
# create a generator
def request_messages():
for i in range(5):
request = demo_pb2.Request(client_id=CLIENT_ID, request_data=("called by Python client, message: %d" % i))
request = demo_pb2.Request(
client_id=CLIENT_ID,
request_data=("called by Python client, message: %d" % i))
yield request
time.sleep(1)
response_iterator = stub.BidirectionalStreamingMethod(request_messages())
for response in response_iterator:
print("recv from server(%d), message=%s" % (response.server_id, response.response_data))
print("recv from server(%d), message=%s" % (response.server_id,
response.response_data))
print("--------------Call BidirectionalStreamingMethod Over---------------")

@ -1,8 +1,7 @@
import grpc
from threading import Thread
from concurrent import futures
import grpc
import demo_pb2_grpc
import demo_pb2
@ -16,8 +15,11 @@ class DemoServer(demo_pb2_grpc.GRPCDemoServicer):
# unary-unary(In a single call, the client can only send request once, and the server can
# only respond once.)
def SimpleMethod(self, request, context):
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!!!!")
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
# 客户端流模式(在一次调用中, 客户端可以多次向服务器传输数据, 但是服务器只能返回一次响应)
@ -26,22 +28,27 @@ class DemoServer(demo_pb2_grpc.GRPCDemoServicer):
def ClientStreamingMethod(self, request_iterator, context):
print("ClientStreamingMethod called by client...")
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")
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
# 服务端流模式(在一次调用中, 客户端只能一次向服务器传输数据, 但是服务器可以多次返回响应)
# unary-stream (In a single call, the client can only transmit data to the server at one time,
# but the server can return the response many times.)
def ServerStreamingMethod(self, request, context):
print("ServerStreamingMethod called by client(%d), message= %s" % (request.client_id, request.request_data))
print("ServerStreamingMethod called by client(%d), message= %s" %
(request.client_id, request.request_data))
# 创建一个生成器
# create a generator
def response_messages():
for i in range(5):
response = demo_pb2.Response(server_id=SERVER_ID,
response_data=("send by Python server, message=%d" % i))
response = demo_pb2.Response(
server_id=SERVER_ID,
response_data=("send by Python server, message=%d" % i))
yield response
return response_messages()
@ -56,13 +63,16 @@ class DemoServer(demo_pb2_grpc.GRPCDemoServicer):
# Open a sub thread to receive data
def parse_request():
for request in request_iterator:
print("recv from client(%d), message= %s" % (request.client_id, request.request_data))
print("recv from client(%d), message= %s" %
(request.client_id, request.request_data))
t = Thread(target=parse_request)
t.start()
for i in range(5):
yield demo_pb2.Response(server_id=SERVER_ID, response_data=("send by Python server, message= %d" % i))
yield demo_pb2.Response(
server_id=SERVER_ID,
response_data=("send by Python server, message= %d" % i))
t.join()

Loading…
Cancel
Save