Django/Celery multiple queues on localhost - routing not working

后端 未结 1 1366
独厮守ぢ
独厮守ぢ 2021-02-05 19:19

I followed celery docs to define 2 queues on my dev machine.

My celery settings:

CELERY_ALWAYS_EAGER = True
CELERY_TASK_RESULT_EXPIRES = 60  # 1 mins
CEL         


        
相关标签:
1条回答
  • 2021-02-05 20:19

    Ok, so i figured it out. Following is my whole setup, settings and how to run celery, for those who might be wondering about same thing as my question did.

    Settings

    CELERY_TIMEZONE = TIME_ZONE
    CELERY_ACCEPT_CONTENT = ['json', 'pickle']
    CELERYD_CONCURRENCY = 2
    CELERYD_MAX_TASKS_PER_CHILD = 4
    CELERYD_PREFETCH_MULTIPLIER = 1
    
    # celery queues setup
    CELERY_DEFAULT_QUEUE = 'default'
    CELERY_DEFAULT_EXCHANGE_TYPE = 'topic'
    CELERY_DEFAULT_ROUTING_KEY = 'default'
    CELERY_QUEUES = (
        Queue('default', Exchange('default'), routing_key='default'),
        Queue('feeds', Exchange('feeds'), routing_key='long_tasks'),
    )
    CELERY_ROUTES = {
        'arena.social.tasks.Update': {
            'queue': 'feeds',
            'routing_key': 'long_tasks',
        },
    }
    

    How to run celery?

    terminal - tab 1:

    celery -A proj worker -Q default -l debug -n default_worker
    

    this will start first worker that consumes tasks from default queue. NOTE! -n default_worker is not a must for the first worker, but is a must if you have any other celery instances up and running. Setting -n worker_name is the same as --hostname=default@%h.

    terminal - tab 2:

    celery -A proj worker -Q feeds -l debug -n feeds_worker
    

    this will start second worker that consumers tasks from feeds queue. Notice -n feeds_worker, if you are running with -l debug (log level = debug), you will see that both workers are syncing between them.

    terminal - tab 3:

    celery -A proj beat -l debug
    

    this will start the beat, executing tasks according to the schedule in your CELERYBEAT_SCHEDULE. I didn't have to change the task, or the CELERYBEAT_SCHEDULE.

    For example, this is how looks my CELERYBEAT_SCHEDULE for the task that should go to feeds queue:

    CELERYBEAT_SCHEDULE = {
        ...
        'update_feeds': {
            'task': 'arena.social.tasks.Update',
            'schedule': crontab(minute='*/6'),
        },
        ...
    }
    

    As you can see, no need for adding 'options': {'routing_key': 'long_tasks'} or specifying to what queue it should go. Also, if you were wondering why Update is upper cased, its because its a custom task, which are defined as sub classes of celery.Task.

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