python学习笔记3-celery分布式任务处理器
import sys
reload(sys)
sys.setdefaultencoding('utf-8’) # 不加这句话,打印中文log会出错 from celery import Celery celery = Celery('tasks', broker='redis://127.0.0.1:6379/0') #选择本地redis db=0 作为消息载体, 第一个参数为任务名称
from celery.utils.log import get_task_logger # 倒入celery 中的log模块
logger = get_task_logger(__name__) @celery.task(bind=True, max_retries=10,
default_retry_delay=1 * 6) # bind 表示开启, max_retries 是重新尝试的次数,default_retry_delay 是默认的间隔时间,尝试的时间
def exec_task_order_overtime(self, order_id): # 订单到期后,执行订单失效的任务
try:
logger.info('===================> exec_task_order_overtime order_id=%s' % order_id)
success = BaseHandler.context_services.order_overtime_task_service.process_over_time(order_id)
if success is False:
logger.error(
'<================order_overtime_task_service.process_over_time Failed, order_id=%s' % order_id)
raise Return(False)
else:
logger.info(
'<=================order_overtime_task_service.process_over_time Success, order_id=%s' % order_id)
except Exception as exc:
logger.info('exec_task_order_overtime retry, order_id=%s' % order_id)
raise self.retry(exc=exc, countdown=3) # 3 秒后继续尝试, 此处的countdown 优先级高于装饰器中的default_retry_delay
from celery import Celery
celery = Celery('tasks', broker='redis://127.0.0.1:6379/0') #消息载体
push_task_id = celery.send_task('tasks.exec_task_order_overtime'
, [order_id] # 参数,必须为list,具体可见源码,第三个可以为dict,我们这里没有使用
, countdown=10) #延时多久执行 推送消息
疑问1:
self (the task type instance)Celery is the name of the current module, this only needed so names can be automatically generated when the tasks are defined in the __main__module.python学习笔记3-celery分布式任务处理器的更多相关文章
- Python学习笔记 - day14 - Celery异步任务
Celery概述 关于celery的定义,首先来看官方网站: Celery(芹菜) 是一个简单.灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具. 简单来看,是一个基于pyt ...
- 【目录】Python学习笔记
目录:Python学习笔记 目标:坚持每天学习,每周一篇博文 1. Python学习笔记 - day1 - 概述及安装 2.Python学习笔记 - day2 - PyCharm的基本使用 3.Pyt ...
- Python学习笔记,day5
Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...
- python学习笔记目录
人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...
- python 学习笔记 13 -- 经常使用的时间模块之time
Python 没有包括相应日期和时间的内置类型.只是提供了3个相应的模块,能够採用多种表示管理日期和时间值: * time 模块由底层C库提供与时间相关的函数.它包括一些函数用于获取时钟时间和处 ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- python学习笔记--Django入门四 管理站点--二
接上一节 python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...
随机推荐
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- CI框架 CodeIgniter 伪静态 htaccess设置和Nginx伪静态方法
众所周知,LAMP代表Linux下Apache.MySQL.PHP这种网站服务器架构:而LNMP指的是Linux下Nginx.MySQL.PHP这种网站服务器架构.LNMP一键安装包可以从网上下载使用 ...
- Sources
作为一个初学者显然是没有能力自己写教程向的文章的 所以就写个整合资源贴整合一下自己学每个知识点的来源 (其实不是很全因为不记得之前看了什么) ————————————————————————————— ...
- 【python】类的继承和多态
比如,我们已经编写了一个名为Animal的class,有一个run()方法可以直接打印: class Animal(object): def run(self): print 'Animal is r ...
- MySQL日志恢复误删记录
1.查询日志是否开启 show variables like"log_"; 2.查询是用的哪个日志文件 show master status; 3.定位是在什么时间误删的 /usr ...
- Relatives
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...
- 51. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 注册页面JS前台校验
运行效果图: HTML代码: <script> function checkForm(){ //校验用户名 //获得用户名文本框的值 var username=document.getEl ...
- 静态修饰符(关键字static)
1.Static修饰的方法或变量通常称为类方法和类属性 2.静态方法中不能使用this和super关键字,也不能做为局部变量使用 3.在静态方法中不能访问非静态成员方法和非静态成员变量,但是在非静态成 ...
- IIS7 rename application or site
rename site 比较容易,在IIS里面就可以直接 rename,也可以用 cmd 的方式 1. 打开 cmd 2. cd C:\Windows\System32\inetsrv 3. appc ...