veighna-global / vnpy_evo

The core module for using VeighNa (vnpy) quant trading platform on the crypto market.
MIT License
189 stars 41 forks source link

连接binance报错如下 #56

Closed yhmickey closed 2 months ago

yhmickey commented 3 months ago

RestClient on error---------- [2024-08-17T09:18:42.106395]: Unhandled RestClient Error:<class 'RuntimeError'> request:request : GET /fapi/v1/time because terminated: headers: None params: None data: None json: None response:

Exception trace: Traceback (most recent call last): File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/vnpy_rest/rest_client.py", line 285, in _process_request response: Response = await self._get_response(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/vnpy_rest/rest_client.py", line 266, in _get_response cr: ClientResponse = await self.session.request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/client.py", line 657, in _request conn = await self._connector.connect( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 564, in connect proto = await self._create_connection(req, traces, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 973, in _createconnection , proto = await self._create_proxy_connection(req, traces, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1472, in _create_proxy_connection return await self._start_tls_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1207, in _start_tls_connection sslcontext = cast(ssl.SSLContext, await self._get_ssl_context(req)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1027, in _get_ssl_context return await self._make_or_get_ssl_context(False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1033, in _make_or_get_ssl_context return await self._made_ssl_context[verified] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Task <Task pending name='Task-1' coro=<RestClient._process_request() running at /Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/vnpy_rest/rest_client.py:285> cb=[_chain_future.._call_set_state() at /Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/asyncio/futures.py:394]> got Future attached to a different loop

RestClient on error---------- [2024-08-17T09:18:42.108887]: Unhandled RestClient Error:<class 'RuntimeError'> request:request : GET /fapi/v1/exchangeInfo because terminated: headers: None params: None data: None json: None response:

Exception trace: Traceback (most recent call last): File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/vnpy_rest/rest_client.py", line 285, in _process_request response: Response = await self._get_response(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/vnpy_rest/rest_client.py", line 266, in _get_response cr: ClientResponse = await self.session.request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/client.py", line 657, in _request conn = await self._connector.connect( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 564, in connect proto = await self._create_connection(req, traces, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 973, in _createconnection , proto = await self._create_proxy_connection(req, traces, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1472, in _create_proxy_connection return await self._start_tls_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1207, in _start_tls_connection sslcontext = cast(ssl.SSLContext, await self._get_ssl_context(req)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1027, in _get_ssl_context return await self._make_or_get_ssl_context(False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jerry/miniconda3/envs/vnpy_evo/lib/python3.12/site-packages/aiohttp/connector.py", line 1033, in _make_or_get_ssl_context return await self._made_ssl_context[verified] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: await wasn't used with future

yhmickey commented 3 months ago

主界面显示Data Websocket API is connected. 但是报以上错误, 无法正常连接.

mac m1 系统.

veighna-global commented 2 months ago

我们已经重新开发实现了vnpy_evo.websocket和vnpy_evo.rest两个模块。

使用线程池 + 同步客户端的方式,替代了官方基于asyncio的实现。

经过我们测试基本解决了各种奇怪的异步问题,同时也方便各种脚本使用。

这里也分享我们团队的经验:

  1. asyncio异步机制,对于网络通讯的并发量有优势,对于速度延时没有优势(甚至是劣势)
  2. 因此asyncio的主要使用场景,是网络服务端的开发(比如FastAPI开发的各种Web服务)
  3. 而VN的交易接口gateway,是典型的网络客户端应用,使用asyncio无异于用大卡车去跑赛道
  4. 因此更合适的方案依然是基于线程池的同步机制(requests和websocket-client)