简介:

Celery  是一个python开发的异步分布式任务调度模块,是一个消息传输的中间件,可以理解为一个邮箱,每当应用程序调用celery的异步任务时,会向broker传递消息,然后celery的worker从中取消息

Celery  用于存储消息以及celery执行的一些消息和结果

对于brokers,官方推荐是rabbitmq和redis

对于backend,也就是指数据库,为了简单一般使用redis

安装:
windows下:

pip install celery
pip install eventlet (运行需要依赖此包)

linux 下:

pip install celery

crontab_task,taobaovideo_task 分别是两个任务,

main.py

from celery import Celery
import os #将settings添加到环境中
os.environ['DJANGO_SETTINGS_MODULE'] = 'TaoBaoVideoProject.settings' #注册异步app 参数是你的项目名称
celery_app = Celery('TaoBaoVideoProject')
#导入配置文件
celery_app.config_from_object('celery_tasks.config')
celery_app.autodiscover_tasks(['celery_tasks.taobaovideo_task','celery_tasks.crontab_task'])

# 最下面这行,每加一个任务,在这里注册写入即可,

config.py

broker_url = "redis://127.0.0.1:6379/5"      #redis设置密码的:redis://xxxxxxx@127.0.0.1:6379/5
result_backend = "redis://127.0.0.1:6379/6"
timezone = 'Asia/Shanghai'

# 这里注意首先需要把redis数据库安装完成

tasks.py中

视图函数中这样调用:

启动命令:

celery -A celery_tasks.main worker -l info -P eventlet

这个则代表启动成功,如果实在服务器上运行的话我们需要添加守护进程,命令如下:

celery multi start w1 -A celery_tasks.main -l info --logfile=./celerylog.log

这个则代表运行成功,并在当前文件夹生成日志文件

查询该任务的执行结果:

具体实现细节可参考:https://www.cnblogs.com/cwp-bg/p/8759638.html

celrey定时任务实现:

tasks.py中:

from celery.task import periodic_task
from utils.crontab_video_method import TaoBaoVideoUpload @periodic_task(run_every=) #90秒执行一次该方法
def crontab_upload_video():
TaoBaoVideoUpload().detection_video()
return '定时任务执行成功'

命令:

celery -A celery_tasks.main beat -l info

成功运行结果如下:

具体方法参考:https://www.cnblogs.com/52forjie/p/9364136.html

最新文章

  1. CSS3-网站导航,transform,transition
  2. Spring配置项<context:annotation-config/>说明
  3. Linux 终端部分重要快捷键
  4. 2008r2 显示桌面图标
  5. 元素定位之Ui-Automator-Viewer的使用
  6. [物理学与PDEs]第1章习题7 载流线圈的磁场
  7. Hibernate查询部分字段并封装到指定类中
  8. POS VB
  9. solr 中文分词器IKAnalyzer和拼音分词器pinyin
  10. CLOB数据类型截取SUBSTR_按开始位置偏移量
  11. jw player 配置参数
  12. Linux查找指令(阮一峰)
  13. STM32 USB FS Core and USB OTG Core
  14. bootstrap -- css -- 图片
  15. js获取上月的最后一天
  16. falsk注册etcd
  17. C# 使用UDP组播实现局域网桌面共享
  18. 【学习笔记】FFT
  19. 本地存储(localStorage、sessionStorage、web Database)
  20. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

热门文章

  1. thinkphp session 跨域问题解决方案
  2. centos6一键安装WordPress
  3. a标签的伪类
  4. 一个继承的 DataGridView
  5. RGB颜色透明度转换
  6. 编写 Model 层的代码
  7. GMM-EM实验结果
  8. SPSS学习笔记之——Kaplan-Meier生存分析
  9. Python web 项目的依赖管理工具
  10. [转] An In-Depth Look at the HBase Architecture - HBase架构深度剖析