Docket

0.18.2 · active · verified Sun Mar 29

Docket is a Python library that provides a distributed background task system for executing Python functions. It is currently at version 0.18.2 and maintains an active development cycle with frequent releases, offering robust solutions for task scheduling and execution.

Warnings

Install

Imports

Quickstart

A minimal example demonstrating how to define an asynchronous task and run it using Docket. This example uses the default in-memory backend.

import asyncio
from docket import Docket, task

@task
async def greet_task(name: str):
    """An example asynchronous task."""
    print(f"Hello, {name}!")
    return f"Greeted {name}"

async def main():
    # Initialize Docket. In a real application, you might configure a backend like Redis.
    docket = Docket()

    # Add the task to be run. This queues it for execution.
    await docket.add(greet_task, name="World")
    print("Task added. Running Docket...")

    # Run Docket. In a long-running service, this would be awaited indefinitely.
    # For a quickstart, we'll run it briefly to allow the task to execute.
    # For simplicity, we'll assume the task completes quickly.
    try:
        await asyncio.wait_for(docket.run(), timeout=5) # Run for max 5 seconds
    except asyncio.TimeoutError:
        print("Docket run timed out after 5 seconds.")

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

view raw JSON →