Procrastinate 是一个开源的 Python 3.7+ 分布式任务处理库,利用 PostgreSQL 来存储任务定义、管理锁和调度任务。它可以在同步和异步代码中使用。
换句话说,从你的主代码中,你以一种特殊的方式调用特定的函数(任务),而不是在现场运行,它们被安排在现在或将来在别处运行。
<strong>mycode.py</strong> import procrastinate
<strong>Make an app in your code</strong> app = procrastinate.App(connector=procrastinate.AiopgConnector())
<strong>Then define tasks</strong> @app.task(queue="sums") def sum(a, b): with open("myfile", "w") as f: f.write(str(a + b))
with app.open(): <strong>Launch a job</strong> sum.defer(a=3, b=5)
<strong>Somewhere in your program, run a worker (actually, it's often a</strong> <strong>different program than the one deferring jobs for execution)</strong> app.run_worker(queues=["sums"])
|
工作者worker将自动运行这个作业,它将创建一个名为myfile的文本文件,其中包含3+5(即8)的结果。
点击标题