Skip to content

Comments

feat: add thread-safe Queue for TurboAPI handlers#18

Open
justrach wants to merge 3 commits intomainfrom
feature/thread-safe-queue
Open

feat: add thread-safe Queue for TurboAPI handlers#18
justrach wants to merge 3 commits intomainfrom
feature/thread-safe-queue

Conversation

@justrach
Copy link
Owner

@justrach justrach commented Feb 7, 2026

Summary

  • Adds Queue, QueueEmpty, QueueFull - a thread-safe in-memory queue that works from Tokio worker threads where asyncio.Queue fails
  • Uses threading.Condition + collections.deque for true thread safety across Python 3.13, 3.13t, and 3.14
  • Pure Python implementation - works with or without the Rust extension

Usage

from turboapi import Queue, QueueEmpty

task_queue = Queue(maxsize=100)

@app.post("/enqueue")
def enqueue(data: dict):
    task_queue.put(data)
    return {"queued": task_queue.qsize()}

@app.get("/dequeue")
def dequeue():
    try:
        item = task_queue.get(timeout=5.0)
        return {"item": item}
    except QueueEmpty:
        return {"error": "timeout"}, 408

API

  • put(item, block=True, timeout=None) / put_nowait(item)
  • get(block=True, timeout=None) / get_nowait()
  • qsize(), empty(), full()
  • task_done(), join(timeout=None)

Test plan

  • 20 tests passing: basic ops, non-blocking, timeouts, thread safety, bounded queues, task_done/join

justrach and others added 3 commits February 7, 2026 18:44
Implements a FIFO queue using threading.Condition + collections.deque
that works from Tokio worker threads where asyncio.Queue fails.

Supports put/get with blocking and timeout, put_nowait/get_nowait,
qsize/empty/full, and task_done/join for completion tracking.

Generated with AI

Co-Authored-By: AI <ai@example.com>
Enables `from turboapi import Queue, QueueEmpty, QueueFull` for
convenient access to the thread-safe queue.

Generated with AI

Co-Authored-By: AI <ai@example.com>
20 tests covering basic operations, non-blocking get/put, timeouts,
concurrent producer/consumer thread safety, bounded queue blocking,
task_done/join pattern, and turboapi import.

Generated with AI

Co-Authored-By: AI <ai@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant