Celery定时任务
来源:互联网 发布:淘宝卖家为什么改价格 编辑:程序博客网 时间:2024/05/20 09:10
Celery 是一个 基于python的分布式任务队列。应用于:
1.需要异步执行的比如耗时任务
2.定时任务等
Celery流程:
产生任务主动任务或者定时任务->
消息代理 Broker(rabbitMQ)->
任务消费者 Worker->
结果存储
安装
Celery:
pip install celery
或者指定组件:
pip install "celery[librabbitmq,redis,json]"
RabbitMQ:
http://blog.topspeedsnail.com/archives/4750
使用celery
配置成应用
目录格式如下
mq_proj
init__.py
celery.py
tasks.py
config.py
celery.py:
#!/usr/bin/python#-*- coding: utf-8 -*-from __future__ import absolute_import,unicode_literalsfrom celery import Celeryapp = Celery('mq_proj', broker= 'amqp://guest:guest@localhost:5672//',#消息代理 backend='redis://localhost',#结果存储 include=['mq_proj.tasks'])#添加tasks.pyapp.config_from_object('mq_proj.config') #加载Celery配置到config.py文件if __name__ == '__main__': app.start()
tasks.py:
#!/usr/bin/python#-*- coding: utf-8 -*-from __future__ import absolute_import,unicode_literalsfrom .celery import app@app.taskdef remove(path): ''' remove file :param path: :return: ''' print(path) #TODO return True
config.py:
#!/usr/bin/python#-*- coding: utf-8 -*-import osfrom datetime import timedeltaBASE_DIR = os.path.dirname(os.path.abspath(__file__)) + '../../'PATH_TEST = 'path_test'CELERY_ENABLE_UTC = False # 不是用UTCCELERY_TIMEZONE = 'Asia/Shanghai'CELERY_TASK_RESULT_EXPIRES = 60*60*24 #任务结果的时效时间CELERYD_LOG_FILE = BASE_DIR + "/log/celery/celery.log" # log路径CELERYBEAT_LOG_FILE = BASE_DIR + "/log/celery/beat.log" # beat log路径CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml'] # 允许接受的格式 一般采用msgpack(快)和json(跨平台)CELERY_RESULT_SERIALIZER = 'json'#读取任务结果格式CELERY_TASK_SERIALIZER = 'msgpack'#任务序列化反序列化采用msgpackCELERYBEAT_SCHEDULE = { 'add-every-30-seconds': { 'task': 'mq_proj.tasks.remove',#任务 'schedule': timedelta(seconds=30),#时间 'args': (PATH_TEST)#参数 },}
启动定时任务:
celery beat -A mq_proj
启动Worker进程:
celery -A mq_proj worker -l info
之后每隔30会自动执行一次tasks.remove
Django
django-celery可实现admin管理任务
0 0
- Celery定时任务
- django-celery定时任务
- Celery定时任务
- celery的定时任务记录
- django-celery动态添加定时任务
- Celery 学习笔记(2)- 定时任务
- Celery+python+redis异步执行定时任务
- 【Python】django-celery执行定时任务
- Celery消息队列----配置定时任务
- django+celery+redis实现运行定时任务
- Django笔记 使用Celery来进行定时Batch任务
- django使用celery定时任务,使用redis和supervisor。
- Django中使用Celery实现定时任务(用djcelery)
- django —— Celery实现异步和定时任务
- 使用celery中的schedule起定时任务,或叫计划任务
- 九周三次课(12月20日) 17.3/17.4 celery多实例 17.5 celery与定时任务
- Celery 分发任务
- celery 异步任务队列
- MySQL
- 数据结构 — 二叉树的线索化
- Ubuntu Fluxbox GUI with x11vnc and Xvfb
- iOS 退出登录
- 661.Convert BST to Greater Tree
- Celery定时任务
- excel字段过多,导入数据库 存在截断问题的解决方法
- HTML5学习笔记 —— JavaScript开发入门
- lua脚本语言学习
- 前端框架Vue(10)——vuex 状态管理
- 陶陶摘苹果
- weasis调节窗位相关代码分析
- Zstack之RSSI信号值获取以及显示
- 使用HttpClient4,post提交multipart/form-data数据