django —— Celery实现异步和定时任务
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 对此环境会有报错, 暂不建议在此环境下使用
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")
- 启动
django和celery
# 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_task 用crontab或interval设置每5s执行一次
- 启动django和celery, 并查看日志
celery -A Demo worker -l debug # 另一个窗口
celery -A Demo beat -l debug --max-interval=10 # 每10s扫描一次djcelery任务
nohup 后台启动
nohup celery -A Demo worker -l debug > abc.log &
django —— Celery实现异步和定时任务的更多相关文章
- Django + Celery 实现动态配置定时任务
哈喽,今天给大家分享一篇Django+Celery实现动态配置定时任务,因为最近也是无意间看到一位大佬关于这块的文章,然后自己觉得不错,也想学习写一下,然后最终实现功能是在前端页面统一管理计划任务,大 ...
- django+celery+redis实现运行定时任务
0.目的 在开发项目中,经常有一些操作时间比较长(生产环境中超过了nginx的timeout时间),或者是间隔一段时间就要执行的任务. 在这种情况下,使用celery就是一个很好的选择. cele ...
- Django+Celery 执行异步任务和定时任务
celery是一个基于python开发的简单.灵活且可靠的分布式任务队列框架,支持使用任务队列的方式在分布式的机器/进程/线程上执行任务调度.采用典型的生产者-消费者模型,主要由三部分组成: 1. 消 ...
- django -- Celery实现异步任务
1. 环境 python==2.7 djang==1.11.2 # 1.8, 1.9, 1.10应该都没问题 celery-with-redis==3.0 # 需要用到redis作为中间人服务(Bro ...
- Django Celery Redis 异步执行任务demo实例
一.windows中安装redis 安装过程见 <在windows x64上部署使用Redis> 二.环境准备 requirements.txt Django==1.10.5 celery ...
- 使用django + celery + redis 异步发送邮件
参考:http://blog.csdn.net/Ricky110/article/details/77205291 环境: centos7 + python3.6.1 + django2.0.1 ...
- Celery+python+redis异步执行定时任务
我之前的一篇文章中写了[Celery+django+redis异步执行任务] 博文:http://blog.csdn.net/apple9005/article/details/54236212 你会 ...
- Django搭配Celery进行异步/定时任务(一)初步搭建
以下需求场景很常见: 1. 用户点击页面按钮,请求后台进行一系列耗时非常高的操作,页面没有响应/一直Loading,用户体验非常不好. 2. 某些数据需要预先处理,每天凌晨的时候进行运算,大约半小时到 ...
- Django+Celery+xadmin实现异步任务和定时任务
Django+Celery+xadmin实现异步任务和定时任务 关注公众号"轻松学编程"了解更多. 一.celery介绍 1.简介 [官网]http://www.celerypro ...
随机推荐
- Weex开发之路(1):开发环境搭建
一.Weex介绍 Weex是阿里巴巴在2016年6月份对外开源的一款移动端跨平台的移动开发工具,Weex的出现让我们的应用既有了Native的性能和H5的动态性,只要通过前端JS语法就能写出同时兼容i ...
- 【虚拟化系列】VMware vSphere 5.1 简介与安装
一. VMware vSphere 5.1简介 vSphere是VMware推出的基于云计算的新一代数据中心虚拟化套件,提供了虚拟化基础架构.高可用性.集中管理.监控等一整套解决方 ...
- Linux中的lo回环接口详细介绍
1.linux的网络接口之扫盲 (1)网络接口的命名 这里并不存在一定的命名规范,但网络接口名字的定义一般都是要有意义的.例如: eth0: ethernet的简写,一般用于以太网接口. wifi0: ...
- linux下开启https
1.在线安装mod_ssl yum -y install mod_ssl 查看openssl 是否安装成功 rpm -qa |grep openssl 2.建立服务器密钥 openssl genrsa ...
- Windbg+Procdump解决w3wp.exe CPU过百问题
最近发布在windows server2012 IIS8.0上的一个WebAPI项目,才几十个人在线,CPU就会出现过百情况,并且CPU一旦过百应用程序池就自动暂停掉,看到这个问题我感觉应该是程序 ...
- python3 bytes 和 string转换
转自:http://www.jb51.net/article/105064.htm 前言 Python 3 最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分. 文本总是 Unicode,由 ...
- [转]Java加密算法
如基本的单向加密算法: BASE64 严格地说,属于编码格式,而非加密算法 MD5(Message Digest algorithm 5,信息摘要算法) SHA(Secure Hash Algorit ...
- 将多个 docx 文件使用 POI 进行合并,生成单个文档,包含图片
1 添加 maven 依赖,需要使用 poi 的依赖项 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad ...
- msf web_delivery模块攻击
目标机:win7 ip:192.168.31.136 攻击机:kai liunx ip:192.168.31.54 一. ...
- destoon入门实例与常见问题
收集了一些destoon入门实例与常见问题,大家做个参考. 链接如下: destoon忘记后台密码怎么办?destoon找回管理员密码 忘记destoon管理员后台账号密码怎么办?解决方法 desto ...