Skip to main content

Fair task scheduling with async pull: WDRR-based priority queue for Python

Project description

PULQ

Fair task scheduling with async pull — WDRR-based priority queue for Python

CI Documentation Status codecov PyPI version

Python versions License Ruff Checked with mypy Typing: Strict

PULQ schedules pull-based work across named priority buckets using Weighted Deficit Round Robin (WDRR) so higher-weight classes get proportionally more CPU time without starving lower classes. Management commands (e.g. STOP) are delivered per worker ahead of normal tasks.

Install

pip install pulq

For HTTP client transport: pip install pulq[http] — see docs/transports.md.

Quick start

PullQueue and Worker use sensible defaults so you can schedule tasks and run a worker with almost no configuration:

import asyncio

from pulq import (
    CommandType,
    InMemoryTaskRepository,
    LocalTransport,
    PullQueue,
    Task,
    Worker,
)

async def handle(task: Task) -> dict:
    return {"ok": True, "echo": task.payload}

async def main() -> None:
    repo = InMemoryTaskRepository()
    queue = PullQueue(repo)  # default: high / medium / low with 3:2:1 weights, quantum 1
    await queue.schedule(Task(priority="high", handler_name="default", payload={"job": "a"}))
    await queue.schedule(Task(priority="low", handler_name="default", payload={"job": "b"}))

    transport = LocalTransport(queue)
    worker = Worker(transport, "worker-1", handle)  # default short backoff when idle

    async def stop_later() -> None:
        await asyncio.sleep(0.05)
        queue.send_command("worker-1", CommandType.STOP)

    await asyncio.gather(worker.run(), stop_later())

asyncio.run(main())

Customizing with Pydantic config

For different priorities, weights, quantum, or worker lifecycle hooks on the handler registry, pass validated config objects:

from pulq import DeficitSchedulerConfig, HandlerRegistry, PullQueueConfig, WorkerConfig

queue = PullQueue(
    repo,
    config=PullQueueConfig(
        scheduler=DeficitSchedulerConfig(
            priority_order=("critical", "high", "low"),
            weights={"critical": 5, "high": 3, "low": 1},
            quantum=2,
        ),
    ),
)

registry = HandlerRegistry(default=handle, startup=my_startup, shutdown=my_shutdown)
worker = Worker(
    transport,
    "worker-1",
    registry,
    config=WorkerConfig(no_work_delay_seconds=0.05),
)

More on HTTP vs local and repositories: docs/transports.md.

Documentation

Full docs: pulq.readthedocs.io

Development

python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
ruff check src tests
mypy src
pytest

Releases (PyPI + Read the Docs): after bumping the version in pyproject.toml and updating CHANGELOG.md, merge to main — see docs/releasing.md for trusted publishing setup on PyPI and manual options.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pulq-0.2.1.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pulq-0.2.1-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file pulq-0.2.1.tar.gz.

File metadata

  • Download URL: pulq-0.2.1.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pulq-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6909507d3ac8fe9b530b719e0a3f8d023a40dc0feb0e63774da5ae78b012f358
MD5 441f54e69c92649faa4878dbe5bd5e63
BLAKE2b-256 d57ade9c0d41c311e4d341f55bada586c66ba45ae71af5e4bacad29fb08011de

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulq-0.2.1.tar.gz:

Publisher: release.yml on vadim-schultz/pulq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pulq-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pulq-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pulq-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49dd8b7d70aa6611e7fed1d46cefe0794fbc88e4d1d1347249f826b0fa52c992
MD5 c2d33f7ed7bf3f04fa7a85d785c2c13b
BLAKE2b-256 1adc07149d954f6e03a5b20abe6e18ddf1ad2e1ff3795351ca0973ddbb17c480

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulq-0.2.1-py3-none-any.whl:

Publisher: release.yml on vadim-schultz/pulq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page