簡體   English   中英

在 Celery 中使用 shared_task 時如何在任務執行之前運行代碼

[英]How do I run code before task execution when using a shared_task in Celery

我有一個要求,我的所有 Celery 任務都必須使用特定的關鍵字參數調用。 我想在執行任務之前檢查並使用關鍵字的值。 例如,假設我有以下內容:

@shared_task
def my_task(*args, **kwargs):
    foo = kwargs.get('bar')  # -> I don't want to copy this to all my tasks
    # Do stuff here

如何創建一個名為my_special_shared_task的新裝飾器,以便以下內容等效於上述內容:

@my_special_shared_task
def my_task(*args, **kwargs):
    # Do stuff here

任務繼承呢? 就像是:

class BaseTask(celery.Task):
    foo = "some_value"


@app.task(base=BaseTask)
def my_task(*args, **kwargs):
    # Do stuff here
    print(self.foo)

是文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM