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