import grpc import time from grpc_proto.check_grpc import check_grpc_pb2_grpc, check_grpc_pb2 def check_server_status(channel): try: health_stub = check_grpc_pb2_grpc.HealthCheckStub(channel) response = health_stub.Check(check_grpc_pb2.HealthCheckRequest(service="TaskService")) return response.status == check_grpc_pb2.HealthCheckResponse.ServingStatus.SERVING except grpc.RpcError as e: print(f"Health check failed: {e}") return False def check_grpc_request(max_retries=3, delay=5): channel = None retries = 0 while retries < max_retries: try: # 创建通道 channel = grpc.insecure_channel('localhost:50051') # 检查服务器状态 if not check_server_status(channel): raise Exception("Server is not healthy") stub = check_grpc_pb2_grpc.TaskServiceStub(channel) # 创建请求消息 request = check_grpc_pb2.TaskRequest( task_id="d6118954-a170-4e1c-84bd-ddbd3114b354", sn="8UUXN6S00A0CK7", content_body=check_grpc_pb2.ContentBody( org_code="HMZHB", func_id=[101204], source_url="xxxxxxxxxx", push_url="", confidence=0.4, para_list=[ check_grpc_pb2.ParaList( func_id=101204, para_invade_enable=True ) ], invade=check_grpc_pb2.Invade( invade_file="meta_data/高压线-0826.geojson", camera_para_url="meta_data/camera_para/hami_camera_para .txt" ) ) ) # 调用远程方法 response = stub.ProcessTask(request) print(f"Response: task_id={response.task_id}, success={response.success}, message={response.message}") return True except grpc.RpcError as e: retries += 1 print(f"RPC error occurred (attempt {retries}/{max_retries}): {e}") if retries < max_retries: print(f"Retrying in {delay} seconds...") time.sleep(delay) except Exception as e: print(f"Error occurred: {e}") retries += 1 if retries < max_retries: print(f"Retrying in {delay} seconds...") time.sleep(delay) finally: if channel: channel.close() print("All retry attempts failed") return False if __name__ == '__main__': check_grpc_request()