Files
big-qmt/grpc/qmt_grpc_new.py
2026-08-31 23:18:04 +08:00

61 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: gbk -*-
import grpc
import qmt_service_pb2
import qmt_service_pb2_grpc
import time
class QmtServiceServicer(qmt_service_pb2_grpc.QmtServiceServicer):
"""实现QMT服务单线程版本"""
def GetAsset(self, request, context):
"""实现GetAsset方法"""
print(f"收到查询请求,账户: {request.account_id}")
# 这里是你调用大QMT API获取数据的逻辑
# 实际使用时请替换为真实的xt_trader查询代码
# 参考: asset = xt_trader.query_stock_asset(acc)
# 模拟数据
total = 1000000.0
cash = 500000.0
market_val = 500000.0
# 模拟一些耗时操作(如查询数据库)
# time.sleep(0.1) # 如果需要可以取消注释
# 返回响应
return qmt_service_pb2.AssetResponse(
total_asset=total,
cash=cash,
market_value=market_val
)
def serve():
"""启动gRPC服务单线程"""
# 使用单线程服务器通过设置maximum_concurrent_rpcs参数限制并发
# 或者使用同步服务器,直接处理请求
server = grpc.server()
# 注册服务
qmt_service_pb2_grpc.add_QmtServiceServicer_to_server(
QmtServiceServicer(),
server
)
# 监听端口
server.add_insecure_port('[::]:58051')
# 启动服务器
server.start()
print("QMT gRPC 服务已启动(单线程模式),监听端口 58051...")
print("所有请求将串行处理,不会并发执行")
# 保持服务运行
try:
server.wait_for_termination()
except KeyboardInterrupt:
print("\n服务已停止")
server.stop(0)
if __name__ == '__main__':
serve()