我应客户请求启动了一项长期运行的后台任务,该任务订阅了一些队列并将所有内容提交给客户。暂时不限制发送到客户端的邮件速率。这是服务器的骨架(我正在使用eventlet
猴子般的步法):
def consume(stream_id: str):
consumer = # subscribe to some queue
try:
while True:
msg = consumer.get()
if (msg is None):
break
socketio.emit('response', msg)
# socketio.sleep(1)
finally:
print("closing consumer")
consumer.close()
@socketio.on('get')
def get(msg):
socketio.start_background_task(consume, msg['stream_id'])
现在的问题是,如果我删除注释的socketio.sleep(1)
,我的任务将通过执行finally
块并在看到client is gone closing socket
日志消息后立即完成。因此,我假设客户端因推送消息的速度而过载。如何在不进行人工睡眠的情况下克服这个问题?客户端除了打印到标准输出外什么都不做。这是python中的客户端代码:
import socketio
sio = socketio.Client()
@sio.event
def connect():
print('connection established')
@sio.event
def response(msg):
print(msg)
@sio.event
def disconnect():
print('disconnected from server')
sio.connect('http://localhost:5000')
sio.emit('get', {'stream_id': "abcd"})
sio.wait()