fix bug
This commit is contained in:
61
grpc/qmt_grpc_new.py
Normal file
61
grpc/qmt_grpc_new.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# -*- 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()
|
||||
Reference in New Issue
Block a user