celery是用python写的一个异步的任务框架,功能非常强大,具体的说明可以查看官网,这里主要提供点demo让你迅速使用该框架
 
1.环境安装
默认安装好了redis
pip install celery
redis 用来作为任务消息的载体
 
2.
tasks.py

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
该文件路径下执行命令, 此后celery 开始作为消费之执行任务
$celery worker -A tasks --loglevel=info
 
生产者呢?执行下面语句后,redis中的db=0 的 celery key是一个 list 类型, 里面存放着执行任务,如果celery没有开启可以清晰看到;开启了celery可能已经被执行了
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:

  有的人会奇怪为什么exec_task_order_overtime有self, 有时候发现没有,区别在与装饰器task,如果@celery.task, 不是装饰器函数调用,则没有self,The bind argument to the task decorator will give access to self (the task type instance)
 
 
疑问2:
  celery 的构造函数的参数,第一个为模块名,也就是本文件名称, 第二个为redis载体地址,看官网介绍
The first argument to 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.
The second argument is the broker keyword argument, specifying the URL of the message broker you want to use. Here using RabbitMQ (also the default option).
 
疑问3:
  celery内存不足时,没有反馈机制:
我们知道socket网络传输中,当接受端来不及处理的时候,发送端会阻塞;celery 完全没有相关机制;
我们不需要发送端阻塞,当然也不能,但是我们的celery来不及处理时,应该缓存一些在redis中,虽然会造成消息处理不及时,但是也不至于内存不足的问题出现,这里可能需要自己做一些处理;
例如:retry次数控制尽可能少,任务执行失败后记录下来,根据业务需要比如10分钟后再次抛入redis消息队列中
 
 
 
 

python学习笔记3-celery分布式任务处理器的更多相关文章

  1. Python学习笔记 - day14 - Celery异步任务

    Celery概述 关于celery的定义,首先来看官方网站: Celery(芹菜) 是一个简单.灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具. 简单来看,是一个基于pyt ...

  2. 【目录】Python学习笔记

    目录:Python学习笔记 目标:坚持每天学习,每周一篇博文 1. Python学习笔记 - day1 - 概述及安装 2.Python学习笔记 - day2 - PyCharm的基本使用 3.Pyt ...

  3. Python学习笔记,day5

    Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...

  4. python学习笔记目录

    人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...

  5. python 学习笔记 13 -- 经常使用的时间模块之time

    Python 没有包括相应日期和时间的内置类型.只是提供了3个相应的模块,能够採用多种表示管理日期和时间值: *    time 模块由底层C库提供与时间相关的函数.它包括一些函数用于获取时钟时间和处 ...

  6. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  7. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  8. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  9. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  10. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

随机推荐

  1. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  2. LeetCode【169. Majority Element】

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. spl_autoload_register更改框架文件引用模式

    今天单点登陆要用到 spl_autoload_register,但是YII的Yii::autoload在包含失败的时候会抛异常,就不会执行(spl_autoload_call)其他spl_autolo ...

  4. SQL Server 2008 收缩日志 清空删除大日志文件 转载

    SQL Server 2008 收缩日志 清空删除大日志文件 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005)Ba ...

  5. SQL Server 2012 配置数据库邮件

    发送和接受邮箱不能用QQ邮箱,可以用163网易邮箱,同时要求要发送邮件的计算机能上外网 查看163网易邮箱的发送和接收服务器的方法如下 在数据库的管理中,右击数据库邮件,选择配置数据库邮件 出现对话框 ...

  6. Makefile中的特殊宏定义以及实用选项

    Makefile中的一些特殊宏定义的名字跟shell中的位置变量挺相似的. $?    当前目标所依赖的文件列表中比当前目标文件还要新的文件 $@   当前目标我名字 $<   当前依赖文件的名 ...

  7. edittext_解释

    ============ 2   android判断EditText输入的数字.中文还是字母方法   String txt = edInput.getText().toString(); Patter ...

  8. RabbitMQ(三)

    官方的使用教程(测试运行) 1."Hello World!" -- 发送接收 We're about to tell the server to deliver us the me ...

  9. COSBench性能测试配置--一张图说明一切

    COSBench性能测试配置--一张图说明一切: 测试配置,并发数,运行时间设置  

  10. google打不开啦,咋办?

    前言:以前开发的时候一直使用google浏览器,好像是两年前的某一天突然间发现google搜索不能访问了,我喜欢将自己觉得有趣的网页做成标签页,google不能访问只能先换别的了,firefox也挺不 ...