3

Goals:

  1. socketio.run(app) launches the server and use a while loop to emit data infinitely to multiple javascripts (clients).

  2. 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:

  1. 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!

ctheadn8954
  • 129
  • 1
  • 8

1 Answers1

5

I'm not sure I understand your question, but if what you are asking is how to start a thread independently of client connections, you can just start it right before you launch the server:

if __name__ == '__main__':
    socketio.start_background_task(my_other_thread)
    socketio.run(app, debug=True, host='localhost', port=args.portNum)

If you need these two threads to be synchronized, as in, when my_other_thread has a new value to send, background_thread immediately picks it up and sends it, then you can use standard thread synchronization primitives. Maybe an Event instance is what you need in this case. See the Python threading docs in the standard library.

Regarding how to allow multiple clients to connect, there is nothing you need to do for that. The server as you have it will allow connections from multiple clients.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • thanks a lot! I am still confused about when you say whenever my_other_thread has new values, background_thread will send the values. Aren't they in the same thread, in socketio.start_background_task(my_other_thread)?? Another question is, say I have multiple background_tasks, can they share data with each other using Queue class like normal python threads do?? – ctheadn8954 Nov 28 '18 at 17:31
  • Yes, you can use a `Queue` to synchronize two or more threads. But if you are using eventlet or gevent you need to use the Queue object that comes with these frameworks, not the one from Python. – Miguel Grinberg Nov 29 '18 at 19:38
  • Thanks, I have successfully used Queue object to synchronize my threads. One point hits me, that is, why can't I use a global variable for sharing data between thread instead of use Queue class? – ctheadn8954 Dec 05 '18 at 16:05
  • You can share data between threads using global variables, that is not a problem. The problem is synchronizing the threads, how to make a thread wake up and do something when another thread reaches certain state. That cannot be done with global variables. – Miguel Grinberg Dec 05 '18 at 19:40