我在芹菜中有两项任务,希望将它们发送给不同的工人来完成,但我不知道如何做到这一点。我已经查看了芹菜文档的task_routes部分,并尝试了stackoverflow中的一些东西,但没有成功。
tasks.py
@app.task
def task1():
does something
@app.task
def task2():
does something else
我有两个芹菜工人,我希望他们每个人专注于一项任务,所以工人1在任务1上,工人2在任务2上
celery.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project')
app.conf.timezone = 'Europe/London'
app.config_from_object('django.conf:settings')
app.conf.update(BROKER_URL=str(os.getenv('REDIS_URL')),
CELERY_RESULT_BACKEND=str(os.getenv('REDIS_URL')),
broker_use_ssl = {
'ssl_cert_reqs': ssl.CERT_NONE
},
redis_backend_use_ssl = {
'ssl_cert_reqs': ssl.CERT_NONE
})
app.autodiscover_tasks()
然后procfile-
web: gunicorn project.wsgi --log-file -
worker1: celery -A project worker -l INFO --concurrency=1 -Ofair -n worker1.%h
worker2: celery -A project worker -l INFO --concurrency=1 -Ofair -n worker2.%h
如何设置队列,使worker1=task1和worker2=task2?
您可以为不同的任务设置两个单独的队列:
# 'your.project.tasks.task2' is same as import path
app.conf.task_routes = {'your.project.tasks.task2': {'queue': 'some_special_queue'}}
并运行Celery作为:
# you may add -Q celery to first command (celery is a default queue name if you didn't specify it)
celery -A project worker -l INFO --concurrency=1 -Ofair -n worker1.%h
celery -A project worker -l INFO --concurrency=1 -Ofair -n worker2.%h -Q some_special_queue
task2
将向some_special_queue
添加消息,并且只有worker2
侦听此队列
所有其他任务将由标准celery
队列处理