高延迟 Python Socket.Io
High Latency Python Socket.Io
我目前正在使用 Python socket.io and aiohttp to make a Server. My client is a nodejs socket.io client。我有一个使用发送函数发送当前时间的线程。来自 connect 事件的消息会立即收到,但来自 Thread 的消息仅在到达 ping_timeout 后才发送。日志显示事件是在达到超时之前发出的。我如何实现线程立即发送数据,而不是在达到超时后。
Client.js
const { io } = require("socket.io-client");
const socket = io("http://127.0.0.1:8080/");
socket.on('message', (data) => {console.log(data)});
Server.py
from threading import Thread
from time import time
from aiohttp import web
import socketio, asyncio
sio = socketio.AsyncServer(ping_timeout=1, logger=True, engineio_logger=True)
app = web.Application()
sio.attach(app)
@sio.event
async def connect(sid, environ):
await sio.send('firstMessage')
async def sendLoop():
while(True):
await sio.send(f'DATA-{time()}')
await asyncio.sleep(0.5)
Thread(target=asyncio.run, args=(sendLoop(),),daemon=True).start()
web.run_app(app)
日志
78wfS8KVIE-JYJRrAAAA: Sending packet OPEN data {'sid': '78wfS8KVIE-JYJRrAAAA', 'upgrades': ['websocket'], 'pingTimeout': 1000, 'pingInterval': 25000}
78wfS8KVIE-JYJRrAAAA: Received packet MESSAGE data 0
emitting event "message" to all [/]
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 2["message","firstMessage"]
78wfS8KVIE-JYJRrAAAA: Received request to upgrade to websocket
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 0{"sid":"etNjKTKYnMWTk3NHAAAB"}
78wfS8KVIE-JYJRrAAAA: Upgrade to websocket successful
emitting event "message" to all [/]
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 2["message","DATA-1646390154.3194933"]
emitting event "message" to all [/]
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 2["message","DATA-1646390154.8345013"]
由于登录仅打印到控制台,因此没有时间戳。然而,连接过程是 <1s
更新
从Python 3.9 切换到 3.7.3
时问题已解决
从 Python 3.9 切换到 3.7.3
时问题已解决
我目前正在使用 Python socket.io and aiohttp to make a Server. My client is a nodejs socket.io client。我有一个使用发送函数发送当前时间的线程。来自 connect 事件的消息会立即收到,但来自 Thread 的消息仅在到达 ping_timeout 后才发送。日志显示事件是在达到超时之前发出的。我如何实现线程立即发送数据,而不是在达到超时后。
Client.js
const { io } = require("socket.io-client");
const socket = io("http://127.0.0.1:8080/");
socket.on('message', (data) => {console.log(data)});
Server.py
from threading import Thread
from time import time
from aiohttp import web
import socketio, asyncio
sio = socketio.AsyncServer(ping_timeout=1, logger=True, engineio_logger=True)
app = web.Application()
sio.attach(app)
@sio.event
async def connect(sid, environ):
await sio.send('firstMessage')
async def sendLoop():
while(True):
await sio.send(f'DATA-{time()}')
await asyncio.sleep(0.5)
Thread(target=asyncio.run, args=(sendLoop(),),daemon=True).start()
web.run_app(app)
日志
78wfS8KVIE-JYJRrAAAA: Sending packet OPEN data {'sid': '78wfS8KVIE-JYJRrAAAA', 'upgrades': ['websocket'], 'pingTimeout': 1000, 'pingInterval': 25000}
78wfS8KVIE-JYJRrAAAA: Received packet MESSAGE data 0
emitting event "message" to all [/]
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 2["message","firstMessage"]
78wfS8KVIE-JYJRrAAAA: Received request to upgrade to websocket
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 0{"sid":"etNjKTKYnMWTk3NHAAAB"}
78wfS8KVIE-JYJRrAAAA: Upgrade to websocket successful
emitting event "message" to all [/]
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 2["message","DATA-1646390154.3194933"]
emitting event "message" to all [/]
78wfS8KVIE-JYJRrAAAA: Sending packet MESSAGE data 2["message","DATA-1646390154.8345013"]
由于登录仅打印到控制台,因此没有时间戳。然而,连接过程是 <1s
更新
从Python 3.9 切换到 3.7.3
时问题已解决从 Python 3.9 切换到 3.7.3
时问题已解决