异步任务用提celery.
队列用的是redis,rabbitmq两种
celery思路:
(1)引用celery
#coding:utf-8
from __future__ import absolute_import from celery import Celery app = Celery('T1',include=['T1.tasks','T1.call_tasks']) app.config_from_object('T1.config') if __name__ == '__main__': app.start()
(2)引用celery配置
#coding:utf-8 from __future__ import absolute_import from datetime import timedelta from time import timezone CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/1' BROKER_URL = 'redis://127.0.0.1:6379/0' CELERY_TASK_SERIALIZER='json' CELERY_ACCEPT_CONTENT=['json'] CELERY_RESULT_SERIALIZER='json' CELERY_TIMEZONE='Europe/Oslo' CELERY_ENABLE_UTC=True CELERYBEAT_SCHEDULE = { 'add-every-5-seconds':{ 'task':'T1.tasks.add', 'schedule':timedelta(seconds=5), 'args':(16,16), }, 'add-every-10-seconds':{ 'task':'T1.call_tasks.call_tasks', 'schedule':timedelta(seconds=10), 'args':(16,16), }, 'add-every-10-seconds':{ 'task':'T1.call_tasks.call_task2', 'schedule':timedelta(seconds=10), 'args':(16,16), } }
(3)
把任务放进celery
#coding:utf-8
from __future__ import absolute_import from T1.celery import app # brokers = 'redis://127.0.0.1:6379/0' # backend = 'redis://127.0.0.1:6379/1' # # app = Celery('tasks',broker=brokers,backend=backend) @app.task def add(x,y): z = x+y return z
(4)
调用的任务也放进celery并定时跑
#coding:utf-8 from __future__ import absolute_import from T1.tasks import add from T1.celery import app import json @app.task def call_tasks(n=100,m=200): r = add.delay(2,6) with open('/Users/admin/devops/Django_T1/celery.txt','wb+') as f: f.write(str(r.ready())+str("结果是:"+str(r.get()))+"\r\n") @app.task def call_task2(n=100,m=200): r = add.delay(3,6) with open('/Users/admin/devops/Django_T1/celery.txt','wb+') as f: f.write(str(r.ready())+str("结果是:"+str(r.get()))+"\r\n")
启动redis
运行celery
celery -A T1 worker -B -l info
运行结果:
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
False结果是:9
True结果是:9
False结果是:9
False结果是:9
False结果是:9
生产者,消费者 放在redis是一次消费,一次取了,就没了
以上是基于redis的队列。下面是基于rabbitmq的队列
思路是一样的,只是安装rabbitmq比较费点时间
rabbitmq 安装及异常处理
http://hugoren.iteye.com/blogs/2303162
reference:
http://www.jianshu.com/p/1840035cb510
http://docs.jinkan.org/docs/celery/index.html