Celery是Python开发的分布式任务调度模块,包含以下组件:

  • Celery Beat: 任务调度器,自带的
  • Celery Worker: 执行任务的消费者,通常设置多个
  • Broker: 消息代理,就是任务队列,我们使用redis
  • Producer: 任务生产者,要执行的函数加上@app.task
  • Result Backend: 结果保存,还是redis

Celery安装

1 pip install celery[redis]   

还要安装redis

1 wget http://download.redis.io/releases/redis-2.8.17.tar.gz
2 tar xzf redis-2.8.17.tar.gz
3 cd redis-2.8.17
4 make
5 
6 #启动
7 ./src/redis-server redis.conf

首先实现一个最简单的应用:

1 from celery import Celery
2 
3 app = Celery('hello', broker='redis://localhost:6379/0')
4 
5 @app.task
6 def hello():
7     return 'hello world'

然后我们使用下面的命令启动celery的worker:

1 celery -A tasks worker --loglevel=info

调度任务

>>> from tasks import add
>>> add.delay(4, 4)
>>> add.delay(4, 4).get()  #获取结果

集成celery到django

在项目下新建celery.py

 1 from __future__ import absolute_import   #解决命名问题
 2  
 3 import os
 4 import django
 5 
 6 from celery import Celery
 7 from django.conf import settings
 8  
 9 os.environ.setdefault('DJANGO_SETTINGS_MODULE','online.settings')   #设置配置文件
10 django.setup()
11  
12 app = Celery('online')
13  
14 app.config_from_object('django.conf:settings')    #制定celery配置文件
15 app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)  #任务
16 app.conf.result_backend = ‘redis://localhost:6379/0’ #结果保持

settings.py配置

1 #配置Broker
2 BROKER_URL = 'redis://127.0.0.1:6379/0'
3 BROKER_TRANSPORT = 'redis'

邮件任务

1 from MxOnline.celery import app
2 
3 @app.task
4 def send_register_email(email, send_type="register"):
5     pass

异步调用

1 #coding:utf-8
2 from django.shortcuts import render
3 from django.http import HttpResponse
4 
5 from .tasks import send_register_email
6 
7 def index(request):
8     send_register_email.delay()
9     return HttpResponse(u"邮件发送成功, 请查收")

最后运行:

1 进入online工程目录运行:
2 celery -A online worker -l debug    # -A 默认寻找目录下的celery模块

 

阿里云

 1 # 阿里云封掉了25端口,这里使用SSL/465发送邮件
 2 # 先安装 django_smtp_ssl
 3 pip install django_smtp_ssl
 4 
 5 # settings 配置
 6 EMAIL_HOST = "smtp.163.com"
 7 EMAIL_PORT = 465
 8 EMAIL_HOST_USER = "******@163.com"
 9 EMAIL_HOST_PASSWORD = '******'
10 EMAIL_FROM = "******@163.com"
11 EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'