在处理从websocket服务器推送到我的服务器的数据时,我遇到了一个长时间(3小时)的延迟(编辑:最初延迟很短,然后一整天都会变长)客户端.py. 我知道它不会被服务器延迟。在
例如,每隔5秒,我就会看到keep-unu-alive日志事件及其相应的时间戳。所以一切顺利。但是当我看到日志中处理的数据帧实际上是服务器发送它的之后的3个小时。我是否在做什么来延迟这个过程?在
我是不是把我的合作计划叫做“让你活着”对吗?keep-unu-alive只是发送给服务器的一条消息,用于保持连接。服务器回显消息。我是不是日志太多了?这是否会延迟处理(我不这么认为,因为我看到日志事件立即发生)。在async def keep_alive(websocket):
"""
This only needs to happen every 30 minutes. I currently have it set to every 5 seconds.
"""
await websocket.send('Hello')
await asyncio.sleep(5)
async def open_connection_test():
"""
Establishes web socket (WSS). Receives data and then stores in csv.
"""
async with websockets.connect(
'wss://{}:{}@localhost.urlname.com/ws'.format(user,pswd), ssl=True, ) as websocket:
while True:
"""
Handle message from server.
"""
message = await websocket.recv()
if message.isdigit():
# now = datetime.datetime.now()
rotating_logger.info ('Keep alive message: {}'.format(str(message)))
else:
jasonified_message = json.loads(message)
for key in jasonified_message:
rotating_logger.info ('{}: \n\t{}\n'.format(key,jasonified_message[key]))
"""
Store in a csv file.
"""
try:
convert_and_store(jasonified_message)
except PermissionError:
convert_and_store(jasonified_message, divert = True)
"""
Keep connection alive.
"""
await keep_alive(websocket)
"""
Logs any exceptions in logs file.
"""
try:
asyncio.get_event_loop().run_until_complete(open_connection())
except Exception as e:
rotating_logger.info (e)
编辑:
从documentation-我想这可能与它有关-但我还没有把这些点联系起来。在The max_queue parameter sets the maximum length of the queue that
holds incoming messages. The default value is 32. 0 disables the
limit. Messages are added to an in-memory queue when they’re received;
then recv() pops from that queue. In order to prevent excessive memory
consumption when messages are received faster than they can be
processed, the queue must be bounded. If the queue fills up, the
protocol stops processing incoming data until recv() is called. In
this situation, various receive buffers (at least in asyncio and in
the OS) will fill up, then the TCP receive window will shrink, slowing
down transmission to avoid packet loss.
编辑:2018年9月28日:我正在测试它,但没有keep-alive消息,这似乎不是问题所在。是否与convert_and_store()函数有关?这是否需要是async def,然后再等待?在
^{pr2}$
编辑10/1/2018:似乎keep alive消息和convert_and_store似乎都有问题;如果我将keep alive消息延长到60秒,convert_and_store将每60秒运行一次。所以convert_and_store正在等待keep_alive()。。。在