Python-模块和包.深入Celery之任务绑定/记录日志/自动重试
来源:互联网 发布:北京大数据培训班 编辑:程序博客网 时间:2024/06/07 00:02
异常处理:
1. 当装饰器@app.task添加bind=True时,被修饰的函数第一个参数被作为任务对象,通过self可获取任务的上下文,get_task_logger其实是调用的进线程安全的logging模块,用于终端打印调试.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2016-12-24 17:07:50
# @Author : 李满满 (xmdevops@vip.qq.com)
# @Link : http://xmdevops.blog.51cto.com/
# @Version : $Id$
from
__future__
import
absolute_import
# 说明: 导入公共模块
import
socket
from
celery.utils.log
import
get_task_logger
# 说明: 导入其它模块
from
..app
import
app
logger
=
get_task_logger(__name__)
@app
.task(bind
=
True
)
def
send_mail(
self
, usr, sub, msg):
logger.info(
'''
task_id : {0.id}
task_args : {0.args!r}
task_kwargs: {0.kwargs!r}
'''
.
format
(
self
.request)
)
try
:
raise
socket.error
except
socket.error, e:
self
.retry(exc
=
e, countdown
=
5
, max_retries
=
3
)
finally
:
pass
说明: 错误处理主要是为了重试一些由于网络抖动等原因导致的任务失败,推荐将每个可重试的任务添加一个重试时间和最大重试次数,其实既可以在self.retry中指定,也可在@app.task修饰器中添加.
注意: 运行时可以celery worker -A work.app -l info,然后打开另一个终端尝试from work.notify.email import send_mail;send_mail.delay('usr', 'sub', 'msg')触发任务,观察日志.
原文转自:乐搏学院http://www.learnbo.com/
- Python-模块和包.深入Celery之任务绑定/记录日志/自动重试
- Python-模块和包.深入Celery之Beat触发定时/周期性任务
- 模块和包.深入Celery之节点管理/任务调度/任务追踪
- Python-模块和包.深入Celery之子任务及原语组式/链式/回调
- 模块和包.深入Celery之应用配置/独立模块配置实践
- python模块学习之模块和包
- celery的定时任务记录
- python celery(任务调度器)
- Python Celery 实现异步任务
- Python之模块和包的使用
- 深入理解Python中的模块、包、递归和正则表达式
- Python之Celery(一)
- Python 模块和包
- Python模块和包
- Python 模块和包
- python模块和包
- python 模块和包
- Python模块和包
- 模块和包.深入Celery之节点管理/任务调度/任务追踪
- 实现Button文字(titleLabel)和图片(imageView)上下排列
- Url参数解析
- 百度BCC挂载 CDS磁盘
- iOS开发系列--通知与消息机制
- Python-模块和包.深入Celery之任务绑定/记录日志/自动重试
- select标签移动端兼容
- [转]OpenCV两张图片的合并
- Android平台架构
- 选择排序
- 旋转图像
- TCP的三次握手与四次挥手
- STM32高效编程
- Python-模块和包.深入Celery之Beat触发定时/周期性任务