I have two threads. They’re initialized like this:
if __name__ == "__main__":
queue = Queue()
producer_thread = Thread(target=producer, args=(queue,), daemon=True)
producer_thread.start()
start_webserver(queue)
server_thread.join()
The webserver runs on Flask and SocketIO. What I’m trying to do is every time something gets added to the queue, the webserver emits an event to everybody. Like this:
producer start_webserver loop start_webserver
|----------------------|--------------------|
add item receive item emit event based on item
So far, I’ve tried creating another thread in start_webserver
which just has a while True
loop checking if the queue isn’t empty and if so emitting it like this:
def start_webserver(queue: Queue):
...
@sio.on("connect")
def _on_sio_conn():
sio.send("hi you connected")
...
stop_event = Event()
def create_queue_monitor():
while True:
if stop_event.is_set():
break
if not queue.empty():
item = queue.get()
print(item)
sio.send("hi you have a thing")
queue_monitor = Thread(target=create_queue_monitor)
queue_monitor.start()
...
The function prints the item when it’s received, but my browser’s console never shows anything except the “hi you connected” message. My receiving code is just an onAny
callback, like this
socketio.onAny((...args) => console.log(...args));
What am I doing wrong? Is there an alternative approach I can use?