3.73K 浏览
0

最近用tqapi测试发现web gui 经常性不显示,

api = TqApi(web_gui=”http://192.168.1.123:7878″, auth=TqAuth(xxx, xxx))

api = TqApi(web_gui=True, auth=TqAuth(xxx, xxx))

控制台都没有输出webgui 的启动结果, 比如文档中的例子:

2019-12-13 10:45:26,468 - INFO - 您可以访问 http://127.0.0.1:62964 查看策略绘制出的 K 线图形。
2019-12-13 10:45:27,422 - INFO - 通知: 与 wss://openmd.shinnytech.com/t/md/front/mobile 的网络连接已建立

tqsdk: 2.3.4

很久之前测试过可以

有没有遇到过的朋友

mayanqiong 已回答的问题 2021年3月22日

类似的问题以前的版本遇到过,目前经过多次测试,tqsdk(2.3.4)仍然不能启动web gui,为方便说明问题 在一台linux机器上做如下测试:
测试前先检查一下端口是否占用:
lsof -i:7171 #无输出
测试代码:
api = TqApi(acc, backtest=TqBacktest(start_dt=datetime.datetime(2021, month=3, day=18, hour=9, minute=0, second=0,
microsecond=0), end_dt=datetime.datetime(2021, month=3, day=18, hour=10, minute=0, second=0,
microsecond=0))
# , web_gui=”:7171″
# , web_gui=”127.0.0.1:7171″
, web_gui=”http://127.0.0.1:7171″
, auth=TqAuth(_tq_auth_user, _tq_auth_password))
klines = api.get_kline_serial(“SHFE.ag2106”, 60)
while True:
# 通过wait_update刷新数据
api.wait_update()
if api.is_changing(klines.iloc[-1], “datetime”):
_cur_data = klines.iloc[-1]
print(‘RECV: ‘ + str(_cur_data[‘datetime’]) + ‘, ‘ + str(_cur_data[‘close’]))
sleep(5)
启动后无web gui启动情况
执行端口占用检查 lsof -i:7171 # 仍旧无输出

用其他端口占用代码测试,如占用端口为7676,
同样如上启动测试代码前查看端口是否被占用:
lsof -i:7676 # 无输出

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
def dataReceived(self, data):
# As soon as any data is received, write it back
self.transport.write(data)

class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()

def demo_twisted():
reactor.listenTCP(7676, EchoFactory())
reactor.run()
运行后再执行端口占用检查:
lsof -i:7676
输出:
root@xxx:~# lsof -i:7676
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 3686 root 8u IPv4 65669 0t0 TCP *:7676 (LISTEN)
可以看到7676端口占用情况

在TqWebHelper.link_httpserver
try — finally 中增加了except,终于捕捉到了异常
‘Only async functions are allowed as web-handlers , got <function TqWebHelper.link_httpserver.. at 0x7f840ad74a60>’

应该就是这个 app.router.add_get(path=’/url’, handler=lambda request: TqWebHelper.httpserver_url_handler(url_response)) 与 函数定义
async def link_httpserver(self) 的 async 有冲突,可能需要handler是async函数才可以,而目前的并不是…

后续的
app.router.add_get(path=’/’, handler=self.httpserver_index_handler)
app.router.add_get(path=’/index.html’, handler=self.httpserver_index_handler)
同样需要async

解决:
使httpserver_url_handler httpserver_index_handler支持 async
async def httpserver_url_handler(self, *args, **kwargs):
url_response = {
“ins_url”: self._api._ins_url,
“md_url”: self._api._md_url,
}
# TODO:在复盘模式下发送 replay_dt 给 web 端,服务器改完后可以去掉
if isinstance(self._api._backtest, TqReplay):
url_response[“replay_dt”] = int(
datetime.combine(self._api._backtest._replay_dt, datetime.min.time()).timestamp() * 1e9)
return TqWebHelper.httpserver_url_handler(url_response)

async def httpserver_index_handler(self, *args, **kwargs):
return web.FileResponse(self._web_dir + ‘/index.html’)

link_httpserver 函数中 app.router.add_get 修改为
app.router.add_get(path=’/url’, handler=self.httpserver_url_handler)
app.router.add_get(path=’/’, handler=self.httpserver_index_handler)
app.router.add_get(path=’/index.html’, handler=self.httpserver_index_handler)
然后就可以启动web gui了

0

您好,可以提供以下您使用的 aiohttp 的版本吗?

kdyi666 发表新评论 2021年3月24日

我降级一下看看

降级了可以
aiohttp==3.7.4.post0

您正在查看2个答案中的1个,单击此处查看所有答案。