| | |
| | | import threading |
| | | import time |
| | | |
| | | from log_module.log import hx_logger_trade_debug |
| | | from log_module.log import hx_logger_trade_debug, hx_logger_trade_loop |
| | | from utils import socket_util |
| | | |
| | | |
| | |
| | | raise Exception("无可用的交易client") |
| | | request_id = __get_request_id(_type) |
| | | try: |
| | | hx_logger_trade_loop.info("请求发送开始:client_id-{} request_id-{}", client[0], request_id) |
| | | root_data = {"type": "cmd", |
| | | "data": data, |
| | | "request_id": request_id} |
| | | root_data = socket_util.encryp_client_params_sign(root_data) |
| | | print("请求前对象", root_data) |
| | | # print("请求前对象", root_data) |
| | | # 添加请求头 |
| | | client[1].sendall(socket_util.load_header(json.dumps(root_data).encode(encoding='utf-8'))) |
| | | result = client[1].recv(1024) |
| | | print("请求发送成功", result.decode(encoding='utf-8')) |
| | | # print("请求发送成功", result.decode(encoding='utf-8')) |
| | | hx_logger_trade_loop.info("请求发送成功:client_id-{} request_id-{}", client[0], request_id) |
| | | except BrokenPipeError as e: |
| | | hx_logger_trade_loop.info("请求发送异常:client_id-{} request_id-{} error-{}", client[0], request_id, str(e)) |
| | | ClientSocketManager.del_client(client[0]) |
| | | raise e |
| | | except Exception as e: |
| | | hx_logger_trade_loop.info("请求发送异常:client_id-{} request_id-{} error-{}", client[0], request_id, str(e)) |
| | | logging.exception(e) |
| | | raise e |
| | | return request_id, client |
| | |
| | | start_time = time.time() |
| | | try: |
| | | while True: |
| | | time.sleep(0.01) |
| | | time.sleep(0.005) |
| | | if request_id in __request_response_dict: |
| | | # 获取到了响应内容 |
| | | result = __request_response_dict.pop(request_id) |
| | | hx_logger_trade_loop.info("请求读取成功:client_id-{} request_id-{}", client[0], request_id) |
| | | return result |
| | | if time.time() - start_time > timeout: |
| | | hx_logger_trade_loop.info("请求读取超时:client_id-{} request_id-{}", client[0], request_id) |
| | | # 读取内容超时才会释放 |
| | | ClientSocketManager.release_client(client[0]) |
| | | raise Exception(f"读取内容超时: request_id={request_id}") |
| | |
| | | |
| | | def set_response(client_id, request_id, response): |
| | | if client_id and request_id: |
| | | hx_logger_trade_loop.info("请求响应:client_id-{} request_id-{}", client_id, request_id) |
| | | # 主动触发 |
| | | __request_response_dict[request_id] = response |
| | | ClientSocketManager.release_client(client_id) |