import threading
import queue
import time
# The worker thread pushes results into a queue, and the main thread reads them.
def worker(q):
for i in range(5):
time.sleep(1)
q.put(f"Callback from thread: {i}")
def main():
q = queue.Queue()
t = threading.Thread(target=worker, args=(q,))
t.start()
while True:
msg = q.get()
print("Main received:", msg)
main()
'''
run:
Main received: Callback from thread: 0
Main received: Callback from thread: 1
Main received: Callback from thread: 2
Main received: Callback from thread: 3
Main received: Callback from thread: 4
'''