I am creating a web app that should update readings pushed to the database in realtime, to do so I have to keep a method running in the background whenever a user gets to the page. The data is user-specific, so just adding a background thread won't help. The tough part though, is that I need to be able to stop the thread on a buttonpress, and start it again right after with different data. I tried creating a solution with eventlet gunicorn and flask-socketio that looks a little like this:
@socketio.on('update', namespace='/update')
def make_thread(*args):
thread = Thread(target=update(args)
thread.daemon = True
thread.start()
def update(*args):
while True:
//code that keeps running to query changes
socket.emit('update_reading', reading)
time.sleep(10)
my problem is that while the code works, and it does update the front-end I can't seem to be able to figure out how exactly to create this thread for one user specifically (if that is even necessary). And how to stop the thread from another socket method and start a new one in its place.