Goals:
socketio.run(app) launches the server and use a while loop to emit data infinitely to multiple javascripts (clients).
The data comes from another while loop, but the loop needs to start once the script runs(independent from the clients connection) for other use.
Current:
For point one, I have the following code already:
def background_thread(): while True: socketio.emit('response',{'data': value},namespace='/test') @socketio.on('connect', namespace='/test') def test_connect(): global thread with thread_lock: if thread is None: thread = socketio.start_background_task(target=background_thread) emit('response', {'data': 'Connected'}) if __name__ == '__main__': socketio.run(app, debug=True, host='localhost', port=args.portNum)
From above, I add a thread only after the clients connect to the server. I don't know how do I achieve point 2 from this point? I am thinking having another thread, but having problems organizing the code so that socketio.start_background_task
can share data infinitely with the default python thread once the clients connect to the server.
An additional question: how to allow multiple clients connect to one server?
Thanks all!