依赖:
pip install django-socio-grpc
pip install grpc_health
健康检查
def _toggle_health(health_servicer: health.HealthServicer, service: str):
next_status = health_pb2.HealthCheckResponse.SERVING
while True:
if next_status == health_pb2.HealthCheckResponse.SERVING:
next_status = health_pb2.HealthCheckResponse.NOT_SERVING
else:
next_status = health_pb2.HealthCheckResponse.SERVING
health_servicer.set(service, next_status)
time.sleep(5)
def _configure_health_server(server: grpc.Server):
health_servicer = health.HealthServicer(
experimental_non_blocking=True,
experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=10),
)
health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
# Use a daemon thread to toggle health status
toggle_health_status_thread = threading.Thread(
target=_toggle_health,
args=(health_servicer, "helloworld.Greeter"),
daemon=True,
)
toggle_health_status_thread.start()
在 grpc_handlers 中添加:
def grpc_handlers(server):
server: grpc.Server
_configure_health_server(server) # 添加健康检查