celery的安装和使用
celery是python开发的分布式任务调度模块,接口简单,开发容易,五分钟就写出一个异步发送邮件的服务,celery本身不含消息服务,它使用第三方消息服务来传递任务,目前,celery支持的消息服务有RabbitMQ,redis甚至是数据库,redis是最佳选择
Windows使用celery只能安装 3.1.25版 pip install celery==3.1.25
编写tasks.py
# tasks.py
import time
from celery import Celery celery = Celery('tasks', broker='redis://localhost:6379/0') @celery.task
def sendmail(mail):
print('sending mail to %s...' % mail['to'])
time.sleep(2.0)
print('mail sent.')
启动celery处理任务
celery -A tasks worker --loglevel=info
tasks是任务文件名,worker任务角色,--loglevel=info 任务日志级别
上面的命令行实际上启动的是worker,如果要放到后台运行,可以扔给supervisor
要在目录下启动
D:\py3code\jintong_day1\ce>celery -A tasks worker --loglevel=info
[2018-05-30 19:37:15,815: WARNING/MainProcess] d:\py3.6\lib\site-packages\celery
\apps\worker.py:161: CDeprecationWarning:
Starting from version 3.2 Celery will refuse to accept pickle by default. The pickle serializer is a security concern as it may give attackers
the ability to execute any command. It's important to secure
your broker from unauthorized access when using pickle, so we think
that enabling pickle should require a deliberate action and not be
the default choice. If you depend on pickle then you should set a setting to disable this
warning and to be sure that everything will continue working
when you upgrade to Celery 3.2:: CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml'] You must only enable the serializers that you will actually use. warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) -------------- celery@E73-PC v3.1.25 (Cipater)
---- **** -----
--- * *** * -- Windows-7-6.1.7601-SP1
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x395deb8
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery [tasks]
. tasks.sendmail [2018-05-30 19:37:15,926: INFO/MainProcess] Connected to redis://localhost:6379/
0
[2018-05-30 19:37:15,995: INFO/MainProcess] mingle: searching for neighbors
[2018-05-30 19:37:17,160: INFO/MainProcess] mingle: all alone
[2018-05-30 19:37:17,183: WARNING/MainProcess] celery@E73-PC ready.
怎么发送任务
在当前目录下打开命令行,进入python
>>> from tasks import sendmail
>>> sendmail.delay(dict(to='celery@python.org'))
<AsyncResult: 1a0a9262-7858-4192-9981-b7bf0ea7483b>
在worker里就可以看到任务处理的消息
[2018-05-30 19:36:13,517: INFO/MainProcess] Received task: tasks.sendmail[815178
90-2406-4756-a4b5-d650ea8cd2e2]
[2018-05-30 19:36:13,517: WARNING/Worker-1] sending mail to celery@hhh
[2018-05-30 19:36:15,524: WARNING/Worker-1] mail sent
[2018-05-30 19:36:15,524: INFO/MainProcess] Task tasks.sendmail[81517890-2406-47
56-a4b5-d650ea8cd2e2] succeeded in 1.9970000000030268s: None
celery默认设置就能满足基本要求,worker以pool模式启动,默认大小为CPU核心数量,缺省化机制为pickle,但可以指定为json,由于python调用UNIX/Linux太容易,所以,用celery作为异步任务框架非常合适
celery还有一些高级用法,比如把多个任务组合成一个原子任务,还有一个完善的监控接口
celery的安装和使用的更多相关文章
- Celery学习---Celery 分布式队列介绍及安装
Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, ...
- airflow 安装配置celery+rabbitmq celery+redis
AirFlow的安装可以参考:https://www.cnblogs.com/braveym/p/11378851.html 这里介绍的是AirFlow 安装配置celery+rabbitmq 和 ...
- celery 学习笔记 01-介绍
celery 学习笔记 01-介绍 celery 是 python 中的常用的任务队列框架,经常用于异步调用.后台任务等工作.celery 本身以 python 写,但协议可在不同的语言中实现,其它语 ...
- Python Celery队列
Celery队列简介: Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 使用 ...
- celery学习之入门
Celery 简介 Celery 是一个简单.灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具.它是一个专注于实时处理的任务队列,同时也支持任务调度. broker:一个消息 ...
- celery学习笔记2
1.定义: Celery是一个异步的任务队列(也叫做分布式任务队列) 2.工作结构 Celery分为3个部分 (1)worker部分负责任务的处理,即工作进程(我的理解工作进程就是你写的python代 ...
- celery异步消息处理框架
Celery 1.什么是Clelery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组 ...
- celery 大量消息的分布式系统 定时任务
Celery 1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 https://www.jia ...
- Celery完成定时任务
1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 celery支持linux,如果windows使用celery ...
随机推荐
- FD_CLOEXEC用法及原因_转
转自:使用FD_CLOEXEC实现close-on-exec,关闭子进程无用文件描述符 我们经常会碰到需要fork子进程的情况,而且子进程很可能会继续exec新的程序.这就不得不提到子进程中无用文件描 ...
- Vim使用技巧(2) -- 插入模式技巧 【持续更新】
组合快捷键 Ctrl + h //删除前一个字母(同退格键) Ctrl + w //删除前一个单词 Ctrl + u //删除到行首 Esc //切换到普通模式 Ctrl + [ //切换到普通模式 ...
- TP view中跳转到某个控制器
#直接用 __MODULE__/控制器名/方法名/参数名/参数 <a href="__MODULE__/Product/list_table/goods_id/<?php ech ...
- pycharm2018.1.4激活破解方法与汉化包-2018年6月19日
记录下来备用,顺便分享给大家,有能力的还是希望能够支持正版!支持正版!支持正版! 方法1:激活服务器,最简单快速(截止2018年6月19日可用) 在激活Jetbrains旗下任意产品的时候选择激活服务 ...
- Photoshop脚本之储存图片
function saveEPS( doc, saveFile ) { var saveOptions = new EPSSaveOptions( ); saveOptions.encoding = ...
- Eclipse 查找
Eclipse 查找 工作空间中查找 Eclipse 查找对话框中可以允许用户在指定工作空间上使用单词或字母模式来查找文件. 或者你可以在指定项目或在 package explorer 视图上选择好指 ...
- RAC中数据文件创建到了本地路径(非系统表空间) 使用rman转移
环境: 11.2.0.1 + RHEL5.8 參考文档ID:1678747.1 1.模拟创建 一节点: SQL> create tablespace tdb datafile '/u02/app ...
- Unity3D学习笔记——组件之Mesh(网格)
Mesh:网格组件.主要用于设置外形和外表. Mesh Filter:网格过滤器.就是为游戏对象添加一个外形. 例:设置外形为Sphere 如果获取的网格拥有蒙皮信患,Unity将自动创建一个skn ...
- Android-ViewPagerIndicator框架使用——使用概要
概要:关于ViewPagerIndicator这个框架,我这里只讲解如何使用,而不去讲解他是如何实现的,所以想了解源码剖析的朋友,这个就可以略过了. ViewPagerIndicator这个框架通过自 ...
- SQL 基本关键字 函数 关联 连接
http://www.w3cschool.cn/sql_having.html w3c中有些SQL的讲解 1 order by 排序 SELECT Company, OrderNumber FROM ...