Celery模块调用
celery可以支持多台不同的计算机执行不同的任务或者相同的任务。
celery分布式应用:多个消息队列(Message Queue),不同的消息可以指定发给不同的MessageQueue,这是通过Exchange实现的。发送消息到 MessageQueue中时,可以指定routing——key,Exchange通过routing_key把消息路由(routes)到不同的MessageQueue中
例子:
Celeryconfing.py from kombu import Exchange,Queue BROKER_URL = "redis://172.16.61.158:6379/1" CELERY_RESULT_BACKEND = "redis://172.16.61.158:6379/2" CELERY_QUEUES = { Queue("default",Exchange("default"),routing_key = "default"), Queue("for_task_A",Exchange("for_task_A"),routing_key = "for_task_A"), Queue("for_task_B",Exchange("for_task_B"),routing_key = "for_task_B"), }
celery_ex1.py
from celery import Celery app = Celery() app.config_from_object("celeryconfig") @app.task def taskA(x,y): return x*y @app.task def taskB(x,y,z): return x+y+z @app.task def add(x,y): return x + y r1 = taskA.delay(10,20) print r1.result r2 = taskB.delay(10,20,30) print r2.result print r2.status r3 = add.delay(100,200) print r3.result print r3.status
启动一个worker指定task
celery -A celery_ex1 worker -l info -Q for_task_A
celery -A celery_ex1 worker -l info -Q for_task_A