是否可以在不使用睡眠条件的情况下保持 gRPC 服务器处于活动状态?

Rag*_*tra 2 python grpc

请参考以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

**************************************

try:
     while True:
         time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)
Run Code Online (Sandbox Code Playgroud)

****************************************

在上面的代码块中,是否可以不使用睡眠条件而服务器仍然处于活动状态?

Eri*_*c G 5

当前,gRPC Python 服务器没有与 C++ 服务器Wait()API等效的API。您将需要保留对 Python 的调用time.sleep,如我们的示例服务器代码

  • 在不久的将来有没有计划添加这个? (2认同)