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

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", payload={"job": "a"}))
    await queue.schedule(Task(priority="low", 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 hooks, pass validated config objects:

from pulq import DeficitSchedulerConfig, PullQueueConfig, WorkerConfig, WorkerHooks

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

worker = Worker(
    transport,
    "worker-1",
    handle,
    config=WorkerConfig(
        no_work_delay_seconds=0.05,
        hooks=WorkerHooks(startup=my_startup, shutdown=my_shutdown),
    ),
)

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.1.2.tar.gz (14.2 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.1.2-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pulq-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ce2078e59111c7851da4007a9403a9b8cca795f775e3f984f2bdcbe667e7801e
MD5 9c047413daf85e33b02b94717df72103
BLAKE2b-256 29cb64084b962f815cf012f873c4e874cb8af1e9be5815e34f5fb3c5299f148d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pulq-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c4adca1b9f39ad0b5b7f5630c3097d752ed74e27944a39ecb6cbc3bf9e046c6
MD5 2ad2feb3b813711e3581f3ddc217a4d0
BLAKE2b-256 2d284a39ee3ecf93b929f55b9a059b965a38bbbbf6d00bbd416ecb0c1b547517

See more details on using hashes here.

Provenance

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