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.0.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.0-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pulq-0.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 3e6248c86d22844c367afffd906d4de3c57589ddb1de7ee01ef08edb38203981
MD5 badea459c1c524854c49b8bb465027eb
BLAKE2b-256 e216d8075c7dc69b84a521f0a56f10a5f8e8598bf8ad225279ecfc4160064033

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulq-0.2.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: pulq-0.2.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a79673330da4adc5fe8a054ae5d662aebbd0e13010be2eaf2175af82477d081
MD5 68a06231e2a62b3a88f422cc22870fd3
BLAKE2b-256 b2bdf9969e7feac69c0267eaf19cbe21ec09e268fdcc6549776951320234c4c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulq-0.2.0-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