Skip to main content

Asynchronous Task Queue Manager

PyPI License: MIT

An efficient and robust asynchronous task queue for Python, built on top of asyncio. It's designed for handling concurrent tasks with support for prioritization, dynamic worker scaling, and graceful shutdowns.

Key Features

  • Asynchronous First: Built from the ground up using Python's modern asyncio library for high-performance I/O-bound tasks.
  • Priority Queues: Supports task prioritization out of the box. Lower priority numbers are processed first.
  • Dynamic Worker Management: Automatically scales worker tasks based on queue size to efficiently process jobs.
  • Sync & Async Task Support: Seamlessly handles both async coroutines and regular synchronous functions. -e Timeout Control: Set a global timeout for the queue to prevent it from running indefinitely.
  • Graceful Shutdown: Configure how the queue handles pending tasks on exit—either cancel them immediately or complete high-priority ones before stopping.
  • Multiple Modes: Run in finite mode for a set batch of tasks or infinite mode for long-running services that continuously process tasks.

Installation

You can install the package from PyPI:

pip install async-queue-manager

Basic Usage

Here's how to get started with the TaskQueue in just a few lines of code.

import asyncio
import time

# 1. Import the TaskQueue
from async_queue_manager import TaskQueue

# 2. Define some tasks (can be async or regular functions)
async def async_task(duration, name):
    """An example asynchronous task."""
    print(f"Starting async task: {name}")
    await asyncio.sleep(duration)
    print(f"✅ Finished async task: {name}")

def sync_task(duration, name):
    """An example synchronous task."""
    print(f"Starting sync task: {name}")
    time.sleep(duration)
    print(f"✅ Finished sync task: {name}")

async def main():
    # 3. Create a TaskQueue instance
    task_queue = TaskQueue()

    # 4. Add tasks to the queue
    print("Adding tasks to the queue...")
    task_queue.add_task(async_task, 1, "A (Low Prio)")
    task_queue.add_task(sync_task, 2, "B (Sync)")
    task_queue.add_task(async_task, 0.5, "C (High Prio)")

    # 5. Run the queue and wait for all tasks to complete
    print("🚀 Starting the queue...")
    start_time = time.monotonic()
    await task_queue.run()
    end_time = time.monotonic()
    
    print(f"🎉 All tasks completed in {end_time - start_time:.2f} seconds!")

if __name__ == "__main__":
    asyncio.run(main())

Advanced Usage

Task Prioritization

You can assign a priority to each task. Tasks with a lower number have a higher priority and will be executed first.

import asyncio
from async_queue_manager import TaskQueue

async def my_task(name):
    print(f"Executing task: {name}")
    await asyncio.sleep(0.1)

async def main():
    queue = TaskQueue()

    # Add tasks with different priorities
    queue.add_task(my_task, "Task A (Priority 5)", priority=5)
    queue.add_task(my_task, "Task B (Priority 10)", priority=10)
    queue.add_task(my_task, "Task C (Priority 1)", priority=1) # Highest priority

    # The queue will execute tasks in this order: C, A, B
    await queue.run()

if __name__ == "__main__":
    asyncio.run(main())

Timeout and Shutdown Policy

You can control how the queue behaves when it times out using queue_timeout and on_exit. You can also mark critical tasks with must_complete=True to ensure they finish even if the queue times out.

  • on_exit='complete_priority' (default): When the timeout is reached, the queue stops accepting new tasks but will wait for any tasks marked must_complete=True to finish. Other tasks are cancelled.
  • on_exit='cancel': When the timeout is reached, the queue immediately cancels all running and pending tasks.
import asyncio
from async_queue_manager import TaskQueue

async def long_running_task(duration, name):
    print(f"Starting task: {name} (will run for {duration}s)")
    await asyncio.sleep(duration)
    print(f"✅ Finished task: {name}")

async def main():
    # Initialize with the 'complete_priority' shutdown policy
    queue = TaskQueue(on_exit='complete_priority')

    # This task will likely be cancelled by the timeout
    queue.add_task(long_running_task, 4, "Normal Task")

    # This task will be allowed to finish because must_complete is True
    queue.add_task(long_running_task, 4, "Critical Task", must_complete=True)

    print("Running queue with a 2-second timeout...")
    await queue.run(queue_timeout=2)
    print("Queue has finished or timed out.")
    # Expected output will show "Critical Task" finishing after the timeout is announced.

if __name__ == "__main__":
    asyncio.run(main())

API Reference

TaskQueue(...)

The main class for managing the queue.

Parameter Type Description Default
size int The maximum size of the queue. 0 means infinite. 0
queue_timeout int Default timeout in seconds for the queue when run() is called. 0 (no timeout)
on_exit 'cancel' or 'complete_priority' The policy for handling tasks on shutdown or timeout. 'complete_priority'
mode 'finite' or 'infinite' 'finite' stops when empty; 'infinite' keeps the queue running indefinitely. 'finite'

TaskQueue.add_task(...)

Adds a new task to the queue.

Parameter Type Description Default
task Callable Coroutine The async or sync function to execute.
*args, **kwargs Any Arguments to pass to the task function.
must_complete bool If True, task is completed even if queue times out (with on_exit='complete_priority'). False
priority int The priority of the task. A lower number means higher priority. 3

await TaskQueue.run(...)

Starts the queue workers and waits for tasks to complete.

Parameter Type Description Default
queue_timeout int Overrides the default timeout for this specific run. None

Contributing

Contributions are welcome! If you'd like to contribute, please follow these steps:

  1. Fork the repository.
  2. Clone your fork and set up the development environment:
    git clone [https://github.com/YOUR_USERNAME/taskqueue.git](https://github.com/YOUR_USERNAME/taskqueue.git)
    cd taskqueue
    pip install -e .[dev]
    
  3. Make your changes and add tests for them.
  4. Run the tests to ensure everything is working:
    pytest
    
  5. Submit a pull request with a clear description of your changes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for async-queue-manager 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async-queue-manager 0.1.1
File Size Uploaded Provenance Actions
async_queue_manager-0.1.1.tar.gz 10.8 kB Without provenance Details for async_queue_manager-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for async-queue-manager 0.1.1
File Interpreter ABI Platform Actions
async_queue_manager-0.1.1-py3-none-any.whl Python 3 none any Details for async_queue_manager-0.1.1-py3-none-any.whl

Total release size: 19.4 kB

Release files / async_queue_manager-0.1.1.tar.gz

Download URL async_queue_manager-0.1.1.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bb2340e4b159f7aadb0d66fc25b4ff8a3eef41e7bd247cfed71f35deafe60df4
BLAKE2b-256 checksum
How to use checksums
692d4f08a7dc60c65012fba8d378c7622a2c30c7297deb189f6706ed91cc3d18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release files / async_queue_manager-0.1.1-py3-none-any.whl

Download URL async_queue_manager-0.1.1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
40688d9a14fb9f0afde72048bfcebe4c8bdce53184cd62a0b5345d7edfe477f6
BLAKE2b-256 checksum
How to use checksums
6d45b0f6a22cf50c589fc97fca453b0285a7987a4ebb108ec5014a01503c7fbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Security for async-queue-manager 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page