Skip to content

Instantly share code, notes, and snippets.

@DaveIW2034
Last active June 20, 2019 02:58
Show Gist options
  • Save DaveIW2034/d944f02b2d08a2f441cf9b99eb423220 to your computer and use it in GitHub Desktop.
Save DaveIW2034/d944f02b2d08a2f441cf9b99eb423220 to your computer and use it in GitHub Desktop.
# celery 任务队列, celery 定制路由, 改进版
from celery import Celery
app_add = Celery('app_add',backend='redis://localhost:6379/0',broker='redis://localhost:6379/0')
from kombu import Exchange, Queue
media_exchange = Exchange('app_reduce', type='direct')
app_add.conf.task_queues = (
Queue('app_reduce', media_exchange, routing_key='app_reduce'),
)
# 启动任务队列时, 可使用缺省队列celery.
# app_add.conf.task_default_queue = 'app_add'
# app_add.conf.task_default_exchange = 'app_add'
# app_add.conf.task_default_routing_key = 'app_add'
@app_add.task
def add(x, y):
return x + y
from add import add
from reduce import reduce
if __name__ == "__main__":
for i in range(2000):
reduce.delay(i, i)
for i in range(2000):
add.delay(i, i)
from add import app_add
@app_add.task(queue='app_reduce')
def reduce(x, y):
return x -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment