Procrastinate:基於PostgreSQL的Python任務佇列

banq發表於2022-01-30

Procrastinate 是一個開源的 Python 3.7+ 分散式任務處理庫,利用 PostgreSQL 來儲存任務定義、管理鎖和排程任務。它可以在同步和非同步程式碼中使用。
換句話說,從你的主程式碼中,你以一種特殊的方式呼叫特定的函式(任務),而不是在現場執行,它們被安排在現在或將來在別處執行。

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


工作者worker將自動執行這個作業,它將建立一個名為myfile的文字檔案,其中包含3+5(即8)的結果。
點選標題

相關文章