是否可以在不使用睡眠条件的情况下让 gRPC 服务器保持活动状态?
Is it posible to keep a gRPC server alive without using a sleep condition?
请参考以下代码:
import grpc
from concurrent import futures
import time
import calculator_pb2
import calculator_pb2_grpc
import calculator
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):
def SquareRoot(selfself, request, context):
response = calculator_pb2.Number()
response.value = calculator.square_root((request.value))
return response
# Creating gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
calculator_pb2_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# The below line is what is keeping the server alive
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
***************************************
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
***************************************
在上面的代码块中,是否可以不使用睡眠条件而服务器仍处于活动状态?
目前 gRPC Python 服务器没有与 C++ 服务器的 Wait()
API 等效的功能。您需要保持对 Python 的 time.sleep
的调用,就像在我们的 example server code.
中一样
请参考以下代码:
import grpc
from concurrent import futures
import time
import calculator_pb2
import calculator_pb2_grpc
import calculator
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):
def SquareRoot(selfself, request, context):
response = calculator_pb2.Number()
response.value = calculator.square_root((request.value))
return response
# Creating gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
calculator_pb2_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# The below line is what is keeping the server alive
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
***************************************
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
***************************************
在上面的代码块中,是否可以不使用睡眠条件而服务器仍处于活动状态?
目前 gRPC Python 服务器没有与 C++ 服务器的 Wait()
API 等效的功能。您需要保持对 Python 的 time.sleep
的调用,就像在我们的 example server code.