【问题标题】:Local-only message queue?仅限本地消息队列?
【发布时间】:2013-04-30 21:11:49
【问题描述】:

我将 Celery 与 RabbitMQ 一起使用。

对于我的用例,我肯定会有一些消息应该在任何给定节点上执行的实例,但是对于一个特定的队列,我需要在其上执行的事情始发服务器。这是因为它适用于仅限本地文件:

@celery.task
def calculate_hash(filename):
    target = Models.objects.get(filename=filename)
    hasher = hashlib.md5()
    with open(filename, "rb") as f:
        chunk = f.read(64 * 1024)
        while len(chunk) > 0:
            hasher.update(chunk)
            chunk = f.read(64 * 1024)
    target.hash = hasher.hexdigest()
    target.save()

显然,上述任务只在filename实际存在的情况下才相关,而且不太可能同时存在于多个节点上。

有没有办法指定给定的任务只在本地执行?

【问题讨论】:

    标签: rabbitmq celery


    【解决方案1】:

    基本上,如果我正确理解您的问题,您应该让一名特定的工作人员从一个特定的队列中读取数据。

    首先,您可以配置队列以在 celeryd 级别上使用,例如以--queues 选项开头:

    celeryd --queues=celery,queue1.mydomain.com
    

    然后,要将任务发送到特定队列,您可以通过设置queue 参数来使用apply_async

    另见:How to make celery retry using the same worker?

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 2011-04-27
      • 1970-01-01
      • 2012-12-21
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多