| | |
| | | |
| | | |
| | | # 网络请求 |
| | | def __request(_type, data, request_id=None, blocking=False, is_pipe=True): |
| | | def __request(_type, data, request_id=None, blocking=False, is_pipe=True, log_enable=True): |
| | | if not request_id: |
| | | request_id = __get_request_id(_type) |
| | | try: |
| | | if log_enable: |
| | | async_log_util.info(hx_logger_trade_loop, "请求发送开始:client_id-{} request_id-{} is_pipe-{}", 0, request_id, |
| | | is_pipe) |
| | | root_data = {"type": _type, |
| | |
| | | trade_cmd_callback(TradeRequest(_type, root_data, request_id)) |
| | | else: |
| | | pipe_trade.send(json.dumps(root_data).encode("utf-8")) |
| | | if log_enable: |
| | | async_log_util.info(hx_logger_trade_loop, "请求发送成功:request_id-{}", request_id) |
| | | except BrokenPipeError as e: |
| | | hx_logger_trade_loop.info("请求发送异常:request_id-{} error-{}", request_id, str(e)) |
| | |
| | | return request_id |
| | | |
| | | |
| | | def __read_response(request_id, blocking, timeout=TIMEOUT): |
| | | def __read_response(request_id, blocking, timeout=TIMEOUT, log_enable=True): |
| | | if blocking: |
| | | start_time = time.time() |
| | | try: |
| | |
| | | if request_id in __request_response_dict: |
| | | # 获取到了响应内容 |
| | | result = __request_response_dict.pop(request_id) |
| | | if log_enable: |
| | | hx_logger_trade_loop.info("请求读取成功: request_id-{}", request_id) |
| | | return result |
| | | if time.time() - start_time > timeout: |
| | | if log_enable: |
| | | hx_logger_trade_loop.info("请求读取超时: request_id-{}", request_id) |
| | | # 读取内容超时才会释放 |
| | | raise Exception(f"读取内容超时: request_id={request_id}") |
| | |
| | | # 设置L2订阅数据 |
| | | def __test_trade_channel(sid): |
| | | request_id = __request("test", |
| | | {"type": "test", "data": {"sid": sid}}, blocking=True) |
| | | return __read_response(request_id, True) |
| | | {"type": "test", "data": {"sid": sid}}, blocking=True, log_enable=False) |
| | | return __read_response(request_id, True, log_enable=False) |
| | | |
| | | |
| | | def parseResponse(data_str): |