RabbitMQ celery config

1639 단어

1.celery_config.py

from kombu import Exchange, Queue

CELERY_TIMEZONE = 'Asia/Shanghai'
CELERY_BROKER = 'pyamqp://guest@localhost//'
CELERY_BACKEND = 'pyamqp://guest@localhost//'

default_exchange = Exchange('default', type='direct')
compute_exchange = Exchange('compute', type='direct')
media_exchange = Exchange('media', type='direct')

#  3 
CELERY_QUEUES = (
    Queue('default', default_exchange, routing_key='default'),
    Queue('scores', compute_exchange, routing_key='compute.score'),
    Queue('images', compute_exchange, routing_key='media.image'),
)

CELERY_DEFAULT_QUEUE = 'default'
CELERY_DEFAULT_EXCHANGE = 'default'
CELERY_DEFAULT_ROUTTING_KEY = 'default'

#      ,  routing_key
CELERY_ROUTES = {

    'tasks.image': {
        'queue': 'images',
        'routing_key': 'media.image'
    },

    'tasks.score': {
        'queue': 'scores',
        'routing_key': 'compute.score'
    }
}
CELERY_IMPORTS = ('tasks')

2.tasks.py

import time
from celery import Celery

app = Celery(broker='pyamqp://guest@localhost//')
app.config_from_object('celeryconfig')


@app.task
def score():
    #time.sleep(20)
    print("processing score")


@app.task
def image():
    #time.sleep(5)
    print("processing image")


@app.task
def common():
    print("processing common")
    time.sleep(3)

좋은 웹페이지 즐겨찾기