You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking at the example for starting the background thread and doing some operation every n seconds and emitting to the client (in the example folder). I've implemented it but I'm having some trouble with what looks like some sort of threading issue. The issue I'm having is that multiple background threads are spawned on the @socketio.on('connect') method below. Here's what I think is happening:
Client A connects to the server
Some time later, client B connects to the server
Each client is having no issues and getting data every n seconds, as specified.
Now imagine gunicorn is restarted.
Both Client A and Client B get a thread from gunicorn and they both try to run the @socketio.on('connect') method, which creates the background thread. But now there's two background threads emitting the same message every n seconds!
How can I fix this? I've put some snippets below of the relevant sections. I was thinking that I spawn the thread once the server starts, but I don't know if that would work if no socket.io clients exists...
background_marketdata_thread=Nonerooms=list()
defbackground_marketdata_broadcast():
""" Emits market data every 5 seconds for every active ETF :return: """whileTrue:
socketio.sleep(5)
all_market_data=queries.get_multiple_market_data(rooms)
app.logger.warning('I am a thread!')
iflen(all_market_data.keys()) !=0:
foretfinrooms:
try:
ifetfinall_market_data:
socketio.emit('mkt_data', all_market_data[etf], room=etf)
exceptExceptionase:
app.logger.exception(e)
@utils.authenticated_only@socketio.on('join')defjoin(data):
""" Joins a room to subscribe to data for that ETF in the dashboard :param data: :return: """globalroomsifdata['etf'] notinrooms:
rooms.append(data['etf'])
join_room(data['etf'])
@utils.authenticated_only@socketio.on('connect')defemit_market_data():
globalbackground_marketdata_threadifbackground_marketdata_threadisNone:
background_marketdata_thread=socketio.start_background_task(target=background_marketdata_broadcast)
And my logs (note the timestamps!) ...
2017-06-29 19:00:19,303 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:19,650 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:19,658 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:24,332 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:24,727 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:24,744 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:29,352 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:29,748 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:29,766 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:34,373 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:34,764 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:34,779 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:39,387 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:39,781 - basezero_flask - WARNING - I am a thread!
2017-06-29 19:00:39,796 - basezero_flask - WARNING - I am a thread!
Yes, I have actually been sloppy about this, because it is meant to be an example only.
The correct way to do this is to use a Lock to create a critical section. This ensures that if two clients are running the connect handler at the same time, only one will start the thread.
Here is a quick example, I will update the application as well:
Hi all,
I was looking at the example for starting the background thread and doing some operation every
n
seconds and emitting to the client (in theexample
folder). I've implemented it but I'm having some trouble with what looks like some sort of threading issue. The issue I'm having is that multiple background threads are spawned on the@socketio.on('connect')
method below. Here's what I think is happening:Each client is having no issues and getting data every
n
seconds, as specified.@socketio.on('connect')
method, which creates the background thread. But now there's two background threads emitting the same message everyn
seconds!How can I fix this? I've put some snippets below of the relevant sections. I was thinking that I spawn the thread once the server starts, but I don't know if that would work if no socket.io clients exists...
And my logs (note the timestamps!) ...
My versions:
I'm also running behind nginx as a reverse proxy but just serving static files. And my Gunicorn conf:
Thanks for your help :)
The text was updated successfully, but these errors were encountered: