Skip to content

Instantly share code, notes, and snippets.

@vrbadev
Last active February 11, 2025 23:13
Show Gist options
  • Save vrbadev/9c009fa89f4b57b9de1a8356b930857e to your computer and use it in GitHub Desktop.
Save vrbadev/9c009fa89f4b57b9de1a8356b930857e to your computer and use it in GitHub Desktop.
Python async callback synchronization using queues
import queue
import threading
import time
import uuid
# Create two queues: one for requests and one for responses
request_queue = queue.Queue()
response_queue = queue.Queue()
# Callback function that puts a request into the request queue and waits for the response
def async_callback():
request_id = str(uuid.uuid4()) # Generate a unique request ID
request = {"id": request_id, "data": "Request Data"}
# Put the request into the request queue
request_queue.put(request)
print(f"Callback: Placed request {request_id} in the queue.")
# Wait for the response
while True:
response = response_queue.get()
if response["id"] == request_id:
print(f"Callback: Received response for request {request_id}.")
break
# Main thread that processes requests and places responses in the response queue
def main_thread(event):
while not event.is_set():
try:
# Check for incoming requests
request = request_queue.get(timeout=1)
print(f"Main Thread: Processing request {request['id']}.")
# Process the request (simulate with sleep)
time.sleep(1)
response = {"id": request["id"], "result": "Response Data"}
# Place the result into the response queue
response_queue.put(response)
print(f"Main Thread: Placed response for request {request['id']} in the queue.")
except queue.Empty:
# Continue the normal function
print("Main Thread: No incoming requests, continuing normal function.")
time.sleep(1)
# Start the main thread
event = threading.Event()
thread = threading.Thread(target=main_thread, args=(event,), daemon=True)
thread.start()
# Simulate the asynchronous callback
for _ in range(3):
async_callback()
time.sleep(2)
time.sleep(3)
# Stop the main thread
event.set()
thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment