跳转到主要内容

基于Postgres的分布式任务处理库

项目描述

Procrastinate:基于 PostgreSQL 的 Python 任务队列

Deployed to PyPI Deployed to PyPI GitHub Repository Continuous Integration Documentation Coverage badge MIT License Contributor Covenant Discord

Procrastinate 正在寻找 更多维护者!

Procrastinate 是一个开源的 Python 3.8+ 分布式任务处理库,利用 PostgreSQL 存储任务定义、管理锁和调度任务。它可以在同步和异步代码中使用,具有 Django 集成,并且易于与 ASGI 框架配合使用。它支持周期性任务、重试、任意任务锁等。

换句话说,从您的主代码中,您以特殊的方式调用特定的函数(任务),而不是立即执行,而是被安排在其他地方执行,现在或将来。

以下是一个示例(如果您想亲自运行代码,请访问 快速入门

# mycode.py
import procrastinate

# Make an app in your code
app = procrastinate.App(connector=procrastinate.SyncPsycopgConnector())

# 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 usually a
# different program than the one deferring jobs for execution)
app.run_worker(queues=["sums"])

工作进程将运行作业,创建一个名为 myfile 的文本文件,其中包含求和 3 + 5(即 8)的结果。

同样,从命令行

export PROCRASTINATE_APP="mycode.app"

# Launch a job
procrastinate defer mycode.sum '{"a": 3, "b": 5}'

# Run a worker
procrastinate worker -q sums

最后,您还可以异步使用 Procrastinate(实际上,这是推荐的使用方式)

import asyncio

import procrastinate

# Make an app in your code
app = procrastinate.App(connector=procrastinate.PsycopgConnector())

# 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"])

Procrastinate 为组合添加了许多有趣的功能。您可以去快速入门部分进行一般性了解,或者去如何操作部分了解特定功能。讨论部分应该能回答您的问题。否则,请随时打开一个 问题

给未来的自己一个提示:在这里简要说明为什么这个项目命名为 "Procrastinate" ;)

接下来该怎么做

完整的 文档 可能是了解这个项目的最佳地方。

如果您遇到错误,或者想要联系,您始终可以打开一个 工单

项目详情


发行历史 发布通知 | RSS 源

下载文件

下载适用于您平台文件的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码发行版

procrastinate-2.14.0.tar.gz (77.2 kB 查看哈希值)

上传时间 源代码

构建发行版

procrastinate-2.14.0-py3-none-any.whl (123.5 kB 查看哈希值)

上传时间 Python 3

由以下支持