调用 python 方法和直接调用 celery 任务的区别?
Difference between calling a python method and directly calling a celery task?
考虑以下情况:
# tasks.py
@app.task
def add(x, y):
return x + y
def add_normal(x, y):
return x + y
# test.py
from tasks import add, add_normal
add(1, 2)
add_normal(1, 2)
如果 add 方法没有变成 celery 任务 (add_normal(1, 2)
),这个 add(1,2)
调用会有什么不同吗?
根据 celery 的文档 Calling Tasks:
Applying the task directly will execute the task in the current process, so that no message is sent:
但是如果任务失败,是否会将任务提交给代理,以便稍后重试?
有没有文档解释的比较清楚?谢谢!
两者相同。如果要将方法发送到任务队列,则需要使用装饰对象的apply_async
或delay
方法。
考虑以下情况:
# tasks.py
@app.task
def add(x, y):
return x + y
def add_normal(x, y):
return x + y
# test.py
from tasks import add, add_normal
add(1, 2)
add_normal(1, 2)
如果 add 方法没有变成 celery 任务 (add_normal(1, 2)
),这个 add(1,2)
调用会有什么不同吗?
根据 celery 的文档 Calling Tasks:
Applying the task directly will execute the task in the current process, so that no message is sent:
但是如果任务失败,是否会将任务提交给代理,以便稍后重试?
有没有文档解释的比较清楚?谢谢!
两者相同。如果要将方法发送到任务队列,则需要使用装饰对象的apply_async
或delay
方法。