How to use priority in celery task.apply_async

前端 未结 1 1013
悲&欢浪女
悲&欢浪女 2021-02-15 13:24

I have a test queue in celery and I have defined a task for it:

@celery_app.task(queue=\'test\', ignore_result=True)
def priority_test(priority):
           


        
1条回答
  •  日久生厌
    2021-02-15 14:06

    In order to have priority working properly you need to properly configure a couple of settings and you need at least version 3.5.0 of RabbitMQ.

    First set the x-max-priority of your queue to 10. From the docs :

    from kombu import Exchange, Queue
    
    app.conf.task_queues = [
        Queue('tasks', Exchange('tasks'), routing_key='tasks',
              queue_arguments={'x-max-priority': 10},
    ]
    

    A default value for all queues can be set using the task_queue_max_priority setting:

    app.conf.task_queue_max_priority = 10
    

    Then configure the following settings:

    CELERY_ACKS_LATE = True
    CELERYD_PREFETCH_MULTIPLIER = 1
    

    By default the prefetch multiplier is 4, which in your case will cause the first 4 tasks with priority 10, 9, 8 and 7 to be fetched before the other tasks are present in the queue. The CELERY_ACKS_LATE setting will cause the tasks to be acknowledged after they have been executed. You can experiment with this setting to see what behaviour you prefer.

    0 讨论(0)
提交回复
热议问题