Procrastinate: PostgreSQL-based Task Queue for Python¶
Procrastinate is looking for additional maintainers!
Procrastinate is an open-source Python 3.8+ distributed task processing library, leveraging PostgreSQL to store task definitions, manage locks and dispatch tasks. It can be used within both sync and async code and integrated to Django.
In other words, from your main code, you call specific functions (tasks) in a special way and instead of being run on the spot, they’re scheduled to be run elsewhere, now or in the future.
Here’s an example (if you want to run the code yourself, head to Quickstart):
# mycode.py
import procrastinate
# Make an app in your code
app = procrastinate.App(connector=procrastinate.AiopgConnector())
# Then define tasks
@app.task(queue="sums")
def sum(a, b):
with open("myfile", "w") as f:
f.write(str(a + b))
with app.open():
# Launch a job
sum.defer(a=3, b=5)
# Somewhere in your program, run a worker (actually, it's often a
# different program than the one deferring jobs for execution)
app.run_worker(queues=["sums"])
The worker will run the job, which will create a text file
named myfile
with the result of the sum 3 + 5
(that’s 8
).
Similarly, from the command line:
export PROCRASTINATE_APP="mycode.app"
# Launch a job
procrastinate defer mycode.sum '{"a": 3, "b": 5}'
# Run a worker
procrastinate worker -q sums
Lastly, you can use Procrastinate asynchronously too:
import asyncio
import procrastinate
# Make an app in your code
app = procrastinate.App(connector=procrastinate.AiopgConnector())
# Define tasks using coroutine functions
@app.task(queue="sums")
async def sum(a, b):
await asyncio.sleep(a + b)
async with app.open_async():
# Launch a job
await sum.defer_async(a=3, b=5)
# Somewhere in your program, run a worker (actually, it's often a
# different program than the one deferring jobs for execution)
await app.run_worker_async(queues=["sums"])
There are quite a few interesting features that Procrastinate adds to the mix. You can head to the Quickstart section for a general tour or to the How-To sections for specific features. The Discussion section should hopefully answer your questions. Otherwise, feel free to open an issue.
The project is still quite early-stage and will probably evolve.
Note to my future self: add a quick note here on why this project is named “Procrastinate”.
- Quickstart
- How to…
- Find a correct place for your app
- Create your connector
- Initiate and terminate the connection to the database
- Define a task
- Defer a job
- Get more context for task execution
- Launch a worker
- Use the command line
- Ensure jobs run sequentially and in order
- Launch a job in the future
- Ensure tasks don’t accumulate in the queue
- Execute multiple jobs at the same time
- Launch a task periodically
- Define a retry strategy on a task
- Add a task middleware
- Get statistics regarding job executions
- Launch a job and/or execute it asynchronously
- Control the way synchronous calls to defer are handled
- Use Procrastinate in a Django application
- Make the most out of the logging system
- Test your code that uses Procrastinate
- Deploy Procrastinate in a real environment
- Migrate the Procrastinate schema
- Monitor Procrastinate in a real environment
- Delete finished jobs
- Retry stalled jobs
- Limit the number of opened connections
- Define custom JSON encoders and decoders
- Set the database schema
- Create modular collections of tasks by using Blueprints
- Discussions
- How does this all work ?
- Why are you doing a task queue in PostgreSQL ?
- There are 14 standards…
- Defining your app at the top level of your program
- About locks
- Asynchronous operations & concurrency
- Procrastinate’s usage of PostgreSQL functions and procedures
- Why is Procrastinate asynchronous at core?
- How stable is Procrastinate?
- Wasn’t this project named “Cabbage” ?
- Thanks PeopleDoc / UKG
- API Reference
- Glossary
- Contributing
- Changelog
- 0.29.0
- 0.28.0
- 0.27.0: Fixing importlib compatibility
- 0.26.0: Fix metadata
- 0.25.2: Re-fixing the deploy workflow
- 0.25.1: friendly error for get_full_path failures
- 0.25.0: Mostly shenanigans
- 0.24.1: Fix deploy workflow
- 0.24.0
- 0.23.0: Arguments for periodic tasks
- 0.22.0: Blueprints, with_connector, list_locks, py3.10 etc.
- 0.21.0: Support for SQLAlchemy
- 0.20.0: Bugfixes, performance, & janitoring
- 0.19.0: define additional context on the worker
- 0.18.2: Fix SQL migration compatibility issue
- 0.18.1: Bugfixes
- 0.18.0: Log format & auto-delete finished jobs
- 0.17.0: Make migrations simpler & safer
- 0.16.0: Avoid Postgres deadlocks when deferring periodic jobs
- 0.15.2: Fixing the release process again
- 0.15.1: Fixing the release process
- 0.15.0: Improve periodic tasks
- 0.14.0: Add Django support
- 0.13.0: Remove pendulum dependency
- 0.12.1: Add missing SQL index
- 0.12.0: Periodic tasks
- 0.11.0: Fix race condition, improve resource usage
- 0.10.0: Fix that lock bug
- 0.9.0: Queueing lock
- 0.8.1: Fix Travis autodeploy !
- 0.8.0: Concurrent tasks and administration shell
- 0.7.1: Fix migration script
- 0.7.0: Cleaning Postgres Connector
- 0.6.0: Migrations
- 0.5.0: Handling connection
- 0.4.0: From Job Store to Connector
- 0.3.0: We’re going async !
- 0.2.1: Metadata update
- 0.2.0: Ground work
- 0.1.1: Documentation, packaging, licensing
- 0.1.0: First release on PyPI