Python websockets - 如何在异步循环超时后连接?
Python websockets - how to connect after timeout in asyncio loop?
我 运行 下面代码中的异步循环使用 websockets 检索数据。有时网络连接断开或服务器无响应,所以我引入了一个 timeout
,它被异常处理程序捕获。 timeout
使我能够在收到连接错误消息之前快速停止连接。
但是,一旦重新建立网络连接或服务器重新联机,我不确定如何再次连接到服务器以便我可以像以前一样继续运行我的循环?
我还收到以下错误 ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
和 websockets.exceptions.ConnectionClosedError: no close frame received or sent
。
import asyncio
import websockets
import json
from concurrent.futures import TimeoutError as ConnectionTimeoutError
async def call_dapi():
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
while True:
try:
msg = {"jsonrpc": "2.0", "method": "public/get_order_book", "id": 1, "params": {"instrument_name": "BTC-PERPETUAL"}, "depth": "5"}
await websocket.send(json.dumps(msg))
response = await asyncio.wait_for(websocket.recv(), timeout=2)
response = json.loads(response)
print(response)
await asyncio.sleep(1)
except ConnectionTimeoutError as e:
print('Error connecting.')
pass
asyncio.ensure_future(call_dapi())
asyncio.get_event_loop().run_forever()
我没试过,但这就是哲学。
import asyncio
import websockets
import json
from concurrent.futures import TimeoutError as ConnectionTimeoutError
async def call_dapi():
while True:
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
live = True
while live:
try:
msg = {"jsonrpc": "2.0", "method": "public/get_order_book", "id": 1, "params": {"instrument_name": "BTC-PERPETUAL"}, "depth": "5"}
await websocket.send(json.dumps(msg))
response = await asyncio.wait_for(websocket.recv(), timeout=2)
response = json.loads(response)
print(response)
await asyncio.sleep(1)
except ConnectionTimeoutError as e:
print('Error connecting.')
live = False
asyncio.ensure_future(call_dapi())
asyncio.get_event_loop().run_forever()
我 运行 下面代码中的异步循环使用 websockets 检索数据。有时网络连接断开或服务器无响应,所以我引入了一个 timeout
,它被异常处理程序捕获。 timeout
使我能够在收到连接错误消息之前快速停止连接。
但是,一旦重新建立网络连接或服务器重新联机,我不确定如何再次连接到服务器以便我可以像以前一样继续运行我的循环?
我还收到以下错误 ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
和 websockets.exceptions.ConnectionClosedError: no close frame received or sent
。
import asyncio
import websockets
import json
from concurrent.futures import TimeoutError as ConnectionTimeoutError
async def call_dapi():
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
while True:
try:
msg = {"jsonrpc": "2.0", "method": "public/get_order_book", "id": 1, "params": {"instrument_name": "BTC-PERPETUAL"}, "depth": "5"}
await websocket.send(json.dumps(msg))
response = await asyncio.wait_for(websocket.recv(), timeout=2)
response = json.loads(response)
print(response)
await asyncio.sleep(1)
except ConnectionTimeoutError as e:
print('Error connecting.')
pass
asyncio.ensure_future(call_dapi())
asyncio.get_event_loop().run_forever()
我没试过,但这就是哲学。
import asyncio
import websockets
import json
from concurrent.futures import TimeoutError as ConnectionTimeoutError
async def call_dapi():
while True:
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
live = True
while live:
try:
msg = {"jsonrpc": "2.0", "method": "public/get_order_book", "id": 1, "params": {"instrument_name": "BTC-PERPETUAL"}, "depth": "5"}
await websocket.send(json.dumps(msg))
response = await asyncio.wait_for(websocket.recv(), timeout=2)
response = json.loads(response)
print(response)
await asyncio.sleep(1)
except ConnectionTimeoutError as e:
print('Error connecting.')
live = False
asyncio.ensure_future(call_dapi())
asyncio.get_event_loop().run_forever()