Python flask websocket emit in线程在不应播放时正在广播';T

Python flask websocket emit in线程在不应播放时正在广播';T,python,multithreading,flask,flask-socketio,Python,Multithreading,Flask,Flask Socketio,我有以下线程发送到正在工作的websocket,但是如果我有两个浏览器窗口(或多个客户端连接),它们都会看到相同的数据被推送。在示例代码中,我甚至不需要指定broadcast=False,但我尝试了,它仍然显示在所有连接的websocket窗口中 @socketio.on('job_submit', namespace='/socket') def job_submit(message): emitter('received job') # start job and kick

我有以下线程发送到正在工作的websocket,但是如果我有两个浏览器窗口(或多个客户端连接),它们都会看到相同的数据被推送。在示例代码中,我甚至不需要指定broadcast=False,但我尝试了,它仍然显示在所有连接的websocket窗口中

@socketio.on('job_submit', namespace='/socket')
def job_submit(message):
    emitter('received job')
    # start job and kick off thread to read and emit output of job (setup in redis list)
    job = background_task.delay()
    runme = test_duplicates(message)
    if runme:
        threads[len(threads) + 1] = {'thread': None, 'thread_lock': Lock()}
        global thread
        thread = threads[len(threads)]['thread']
        with threads[len(threads)]['thread_lock']:
            if thread is None:
                thread = socketio.start_background_task(output_puller, job)
        threads[len(threads)]['thread'] = thread
        threads[len(threads)]['thread'].setName(message['data'])


def output_puller(job):
    while job.state != 'SUCCESS' and job.state != 'FAILURE':
        result = r_server.lpop(job.id)
        if result:
            socketio.emit('my_response', {'data': result.decode()}, namespace='/socket', broadcast=False)
            print(result)

默认情况下,socket.io将向所有连接的用户广播。如果要向特定客户端广播,则需要在connect上获取会话id:

下面是一个烧瓶示例

@io.on('connected')
def connected():
    print "%s connected" % (request.namespace.socket.sessid)
    clients.append(request.namespace)
当你想发送的时候

clients[k].emit('message', "Hello at %s" % (datetime.now()))

您可以告诉它发送到哪个客户端,而不是全局emit

request.namespace对我来说是一个字符串,因此我无法使用.socket查看您是否有request.sid。检查roomsI do have request.sid下的部分。我将它添加到了我的emit中,现在它工作正常。为了将来的参考,请使用socketio.emit('my_response',{'data':result.decode()},namespace='/socket',room=sid)