使用 redis 和 celery 向所有工作人员广播任务

Broadcast task to all workers with redis and celery

我试试看: main.py

import tasks
if __name__ == '__main__':
    result = tasks.add.apply_async(([4, 4]), queue='broadcast_tasks')
    result.ready()
    value = result.get()
    print(value)

tasks.py

from celery import Celery
from kombu.common import Broadcast
app = Celery('tasks',
             broker='redis://localhost:6379/0',
             backend='redis://localhost:6379/1',
             include=['tasks'])
app.conf.update(
    result_expires=3600,
)
task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
timezone = 'UTC'

app.conf.task_queues = (Broadcast('broadcast_tasks'),)
app.conf.task_routes = {'tasks.add': {'queue': 'broadcast_tasks'}}


@app.task
def add(x, y):
    return x + y

我想向所有使用 redis(broker 和后端)和 celery 的 worker 广播任务,但我没有实现,你能帮我吗?

celery 团队正在合并相应的问题: https://github.com/celery/celery/pull/3934

这应该在下一个版本中可用(我希望)。