子任务是否继承父任务的队列?
Do subtasks inherit the queue of their parent task?
当用Celery创建一个子任务(即弦、链、组)时,你有多个队列(即高优先级、低优先级),子任务是否继承创建它的任务的路由参数?
一句话:没有。
该任务将使用记录的路由 here
在实际阅读源代码后回答我自己的问题...
简答
没有
长答案
使用 mytask.s()
和 mytask.si()
实例化的任务调用 celery.app.Task.subtask()
(called signature()
in master), which does not set any routing information. Compare this to retry()
which calls subtask_from_request()
, which sets the queue from request.delivery_info
。
我的解决方案
子类任务以将队列添加到 Task.subtask()
。
class Task(CeleryTask):
"""
Override the Celery Task baseclass to send subtasks to the same
queue as the main task.
"""
def subtask(self, args=None, kwargs=None, options=None,
*starargs, **starkwargs):
kwargs = kwargs or {}
options = options or {}
# override the queue if not passed in as an option
if set(('queue', 'exchange', 'routing_key')) & set(options.keys()):
options.update(self.request.delivery_info)
return super().subtask(args, kwargs, options,
*starargs, **starkwargs)
通过将 base=Task
传递给 @task
/@shared_task
使用。
当用Celery创建一个子任务(即弦、链、组)时,你有多个队列(即高优先级、低优先级),子任务是否继承创建它的任务的路由参数?
一句话:没有。 该任务将使用记录的路由 here
在实际阅读源代码后回答我自己的问题...
简答
没有
长答案
使用 mytask.s()
和 mytask.si()
实例化的任务调用 celery.app.Task.subtask()
(called signature()
in master), which does not set any routing information. Compare this to retry()
which calls subtask_from_request()
, which sets the queue from request.delivery_info
。
我的解决方案
子类任务以将队列添加到 Task.subtask()
。
class Task(CeleryTask):
"""
Override the Celery Task baseclass to send subtasks to the same
queue as the main task.
"""
def subtask(self, args=None, kwargs=None, options=None,
*starargs, **starkwargs):
kwargs = kwargs or {}
options = options or {}
# override the queue if not passed in as an option
if set(('queue', 'exchange', 'routing_key')) & set(options.keys()):
options.update(self.request.delivery_info)
return super().subtask(args, kwargs, options,
*starargs, **starkwargs)
通过将 base=Task
传递给 @task
/@shared_task
使用。