Docket
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
- breaking From version 0.18.0, Docket's internal dependency system was extracted into the `uncalled-for` library. While intended to be a transparent change for most users, deep or custom integrations with Docket's dependency resolution might require review.
- gotcha Docket tasks are fundamentally asynchronous (`async def`) and rely on `asyncio`. Incorrectly mixing synchronous code in an async task, or blocking the event loop, can lead to performance issues or deadlocks.
- gotcha When using `Cron` dependencies for task scheduling, ensure careful consideration of timezones. Default behavior might rely on the system's local timezone, which can lead to unexpected execution times in distributed or timezone-aware applications.
- gotcha Using persistent backends (e.g., Redis, PostgreSQL) requires robust configuration and connection management to ensure tasks are reliably stored, retrieved, and processed across application restarts and distributed environments.
Install
-
pip install docket
Imports
- Docket
from docket import Docket
- task
from docket import task
- Cron
from docket.dependencies import Cron
Quickstart
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())