Skip to content

If i emit a event in a Thread, the client can't get the event. #549

@Abbyyan

Description

@Abbyyan

I'm trying to do as following.

(1) send message from client to server, add the message into a Queue on the server side.
(2) the server process the data in the queue in a Thread. 
(3) The server emit a response to the client after processing data

The server.py is as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import eventlet
import socketio
from time import sleep
import threading
import queue
q = queue.Queue()


sio = socketio.Server(logger=True, engineio_logger=True)
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})


@sio.event
def connect(sid, environ):
    print('new client connect ', sid)


@sio.event
def request(sid, data):
    sio.emit('client_response', data)
    print("(3)"+str(data))


@sio.event
def response(sid, data):
    print("(2) "+str(data))
    info = {"sid": sid, "data": data}
    q.put(info)


def process_data():
    while True:
        if not q.empty():
            item = q.get()
            data = item.get('data', "")
            sid = item.get('sid', None)
            print("data here: %s" % data)
            sleep(1)
            data = "data send from server "+str(data)
            request(sid, data)



@sio.event
def disconnect(sid):
    print('disconnect ', sid)


threading.Thread(target=process_data).start()

if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('x.x.x.x', 8081)), app)

The client.py is as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socketio
sio = socketio.Client(logger=True)
sio.connect('http://x.x.x.x:8081')


@sio.event
def connect():
    print('connection established')


@sio.event
def client_request(data):
    print("(1) %s" % data)
    sio.emit('response', data)


@sio.event
def client_response(data):
    print("(4) %s" % data)




if __name__ == '__main__':
    for i in range(0, 2):
                client_request("id: "+str(i))
    sio.wait()

The server can get the message send from client, and can process data. But after the server do sio.emit('client_response', data) in the Thread, the client can't receive the processed data from server (If i do the emit in the main thread, it works well ). Could you please give me some suggestions about this please? Thanks a lot.
The log is as follows:
On server side:

% python server.py
Server initialized for eventlet.
(617) wsgi starting up on http://x.x.x.x:8081
(617) accepted ('x.x.x.x', 44067)
a721b61ce62345d399db3006ccf2a769: Sending packet OPEN data {'sid': 'a721b61ce62345d399db3006ccf2a769', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 25000}
new client connect  a721b61ce62345d399db3006ccf2a769
a721b61ce62345d399db3006ccf2a769: Sending packet MESSAGE data 0
x.x.x.x - - [22/Sep/2020 16:47:36] "GET /socket.io/?transport=polling&EIO=3&t=1600764456.5817175 HTTP/1.1" 200 371 0.026230
(617) accepted ('x.x.x.x', 44068)
a721b61ce62345d399db3006ccf2a769: Received request to upgrade to websocket
a721b61ce62345d399db3006ccf2a769: Upgrade to websocket successful
a721b61ce62345d399db3006ccf2a769: Received packet PING data None
a721b61ce62345d399db3006ccf2a769: Sending packet PONG data None
a721b61ce62345d399db3006ccf2a769: Received packet MESSAGE data 2["response","id: 0"]
received event "response" from a721b61ce62345d399db3006ccf2a769 [/]
(2) id: 0
a721b61ce62345d399db3006ccf2a769: Received packet MESSAGE data 2["response","id: 1"]
data here: id: 0
received event "response" from a721b61ce62345d399db3006ccf2a769 [/]
(2) id: 1
emitting event "client_response" to all [/]
a721b61ce62345d399db3006ccf2a769: Sending packet MESSAGE data 2["client_response","data send from server id: 0"]
(3)data send from server id: 0
data here: id: 1
emitting event "client_response" to all [/]
a721b61ce62345d399db3006ccf2a769: Sending packet MESSAGE data 2["client_response","data send from server id: 1"]
(3)data send from server id: 1

On client side:

% python client.py
Engine.IO connection established
Namespace / is connected
(1) id: 0
Emitting event "response" [/]
(1) id: 1
Emitting event "response" [/]
Engine.IO connection dropped
Connection failed, new attempt in 1.03 seconds
Engine.IO connection established
Namespace / is connected
connection established
Reconnection successful
Engine.IO connection dropped

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions