1. 环境

python==2.7

djang==1.11.2  # 1.8, 1.9, 1.10应该都没问题

celery-with-redis==3.0  # 需要用到redis作为中间人服务(Broker)
celery==3.1.25 # 安装上面的会自动安装
kombu==3.0.37
billiard==3.3.0.23 django-celery==3.2.2 # celery插件, 实现定时任务

celery>=4.0 不支持windows, 具体参考官方文档

2. 安装

pip install django==1.11.2 celery-with-redis==3.0 django-celery==3.2.2

3. 安装Redis, 用作Broker (RabbitMQ 官方推荐, 但安装麻烦点)

教程很多, 略

4. 新建django项目

官方文档

- Demo
- Demo
setting.py
wsgi.py
urls.py
- app
- migrations
models.py
views.py
...
  • 配置settings.py
INSTALLED_APPS = (
...
'app',
'djcelery',# django-celery 可以在admin后台定义定时任务, 开始前需要直接migrate
) BROKER_URL = 'redis://127.0.0.1:6379/0'
BROKER_TRANSPORT = 'redis' CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' # 数据库调度
  • 新建文件Demo/Demo/celery.py
from __future__ import absolute_import

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Demo.settings') from django.conf import settings # noqa app = Celery('Demo') # Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
  • 新建Demo/app/tasks.py
from Demo.celery import app

@app.task
def cus_task(*arg):
print('This is a test task')
  • 编辑Demo/app/views.py
from django.shortcuts import render, HttpResponse

from .tasks import cus_task

def index(request):
cus_task.delay()
return HttpResponse("Test async task")
  • 启动djangocelery
# django
python manage.py runserver # celery
celery -A Demo worker -l debug

admin后台中配置celery计划

  • 配置
# 如上settings.py中的设置, 后执行
python manage.py migrate djcelery
  • 登陆admin后台进行配置
# Djcelery模块列表

Crontabs  # 同linux crontab
Intervals # 间隔
Periodic tasks # 周期任务
Tasks
Workers

配置一个periodic task任务内容 app.tasks.cus_taskcrontabinterval设置每5s执行一次

  • 启动django和celery, 并查看日志
celery -A Demo worker -l debug

# 另一个窗口
celery -A Demo beat -l debug --max-interval=10 # 每10s扫描一次djcelery任务

补充

  • [ ] 安装flower
pip install flower
  • [ ] 启动flower(默认启动一个webserver:5555)
python manage.py celery flower
  • [ ] 浏览器打开 http://localhost:5555查看具体

django -- Celery实现异步任务的更多相关文章

  1. Django+Celery 执行异步任务和定时任务

    celery是一个基于python开发的简单.灵活且可靠的分布式任务队列框架,支持使用任务队列的方式在分布式的机器/进程/线程上执行任务调度.采用典型的生产者-消费者模型,主要由三部分组成: 1. 消 ...

  2. Django Celery Redis 异步执行任务demo实例

    一.windows中安装redis 安装过程见 <在windows x64上部署使用Redis> 二.环境准备 requirements.txt Django==1.10.5 celery ...

  3. django —— Celery实现异步和定时任务

    1. 环境 python==2.7 djang==1.11.2 # 1.8, 1.9, 1.10应该都没问题 celery-with-redis==3.0 # 需要用到redis作为中间人服务(Bro ...

  4. 使用django + celery + redis 异步发送邮件

    参考:http://blog.csdn.net/Ricky110/article/details/77205291 环境: centos7  +  python3.6.1 + django2.0.1  ...

  5. django celery异步框架

    描述:实现运维平台的异步执行与定时任务,以下简单描述了安装过程及使用.   安装django和celery pip install django pip install celery pip inst ...

  6. Django --- celery异步任务与RabbitMQ模块

    一 RabbitMQ 和 celery 1 celery Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务, ...

  7. Django使用Celery进行异步任务

    Celery Celery是一个功能完备即插即用的异步任务队列系统.它适用于异步处理问题,当发送邮件.或者文件上传, 图像处理等等一些比较耗时的操作,我们可将其异步执行,这样用户不需要等待很久,提高用 ...

  8. Django+Celery+xadmin实现异步任务和定时任务

    Django+Celery+xadmin实现异步任务和定时任务 关注公众号"轻松学编程"了解更多. 一.celery介绍 1.简介 [官网]http://www.celerypro ...

  9. celery介绍、架构、快速使用、包结构,celery执行异步、延迟、定时任务,django中使用celery,定时更新首页轮播图效果实现,数据加入redis缓存的坑及解决

    今日内容概要 celery介绍,架构 celery 快速使用 celery包结构 celery执行异步任务 celery执行延迟任务 celery执行定时任务 django中使用celery 定时更新 ...

随机推荐

  1. tomcat配置说明,配置不用访问工程名

    # 配置项目访问不用输入项目名称 # [重要]亲测 <Host>中的 appBass="" 一定不能带目录,必须为空,因为启动tomcat会启动appBass下面的所有 ...

  2. SSL编程(3).NET实现SSL服务端

      准备开发用数字证书 一般学习和开发调试场合,不会随便使用正式的SSL服务器证书的私钥.由于服务器验证对于SSL来说是必须的,SSL服务器端必须有拥有一个服务器 证书,即能够访问到证书的私钥.对于要 ...

  3. 微信公众号DOM的一个坑

    最近不知道写什么,node的源码有点不知道怎么入手,还在努力学习C++中…… 在写微信公众号的时候遇到了一个小bug,有一个tab栏,在开发者工具.IOS手机上都OK,但是一到我的小米note上就GG ...

  4. Docker的下载与安装

    一丶下载 1.win10之外的 Docker下载地址: https://www.docker.com/products/docker-toolbox 2.win10 Docker下载地址: https ...

  5. mysql的密码问题

    常见问题 Access denied for user 'root'@'localhost' (using password: YES) 因密码不对而拒绝访问,再jdbc配置中 常常忽略user和密码 ...

  6. java通过url在线预览Word、excel、ppt、pdf、txt文档

    java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...

  7. 素数回文(hdu1431)

    素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. Linux常用基本命令(tail )

    tail命令 作用:从文件的尾部查看,跟head命令作用相反,参数也差不多,默认显示后面10行 格式: tail [option] [file] -n : 显示行数 -c<字节数> gho ...

  9. POJ2227(优先队列)

    The Wedding Juicer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3440   Accepted: 155 ...

  10. PHP批量导出数据为excel表格

    之前用插件phoexcel写过批量导入数据,现在用到了批量导出,就记录一下,这次批量导出没用插件,是写出一个表格,直接输出 //$teacherList 是从数据库查出来的二维数组 $execlnam ...