http://www.mamicode.com/info-detail-1798782.html

https://blog.csdn.net/lu1005287365/article/details/52315786

本系列文章的开发环境:

window 7 + python2.7 + pycharm5 + celery3.1.25 + django1.9.4

在我们日常的开发工作中,经常会遇到这几种情况:

1、在web应用中,用户触发一个操作,执行后台处理程序,这个程序需要执行很长时间才能返回结果。怎样才能不阻塞http请求,不让用户等待从而提高用户体验呢?
2、定时任务脚本:生产环境经常会跑一些定时任务脚本,假如你有上千台的服务器、上千种任务,定时任务的管理很困难,如何对job进行有效的管理?
3、异步需求:比如发送短信/邮件、推送消息、清理/设置缓存?

如果你有以上的需求,那么Celery可能对你很有用。

Celery - 分布式任务队列系统

Celery是一个可以处理大量消息的分布式任务系统,它凭借简单、灵活、可靠的特性被广泛使用。Celery聚焦于实时处理任务,同时也支持定时的任务调度。

1、特性:

  • 查看定时任务的执行情况,比如执行是否成功、当前状态、执行任务花费的时间等。
  • 易于其他框架集成,如使用django管理后台添加、更新、删除任务。
  • 方便把任务和配置管理相关联。
  • 可选多进程、Eventlet和Gevent三种模式并发执行。
  • 提供错误处理机制。

2、架构图

从上图中可以知道Celery包含如下组件:

  • Producer:凡是调用了Celery API、函数或装饰器而产生任务并交给任务队列处理的都是任务生产者。
  • 任务调度组件:Beat进程会读取配置文件的内容,周期性地将配置中到期需要执行的任务发送给任务队列。
  • Celery Worker:负责执行任务的线程,可以在多台服务器运行提高执行效率。
  • Broker:消息中间件,负责接受任务生产者的任务,并且转发work进行执行。Celery目前支持RabbitMQ、Redis、MongoDB、Beanstalk、SQLAlchemy、Zookeeper等作为消息中间件,官方推荐使用RabbitMQ。
  • Result Backend:任务处理完后保存状态信息和结果,以供查询。Celery默认已支持Redis、RabbitMQ、MongoDB、Django ORM、SQLAlchemy等方式。

开始使用Celery

这里Broker和Result Backend都选择RabbitMQ。

1、安装

1) 安装RabbitMQ

2)  安装Celery 3.1.25

为什么选择这个低版本?请见最下面的问题列表。

pip install celery==3.1.25

2、一个简单例子

  2.1) 在一个目录中创建tasks.py文件,内容如下:

from celery import Celery

#创建celery实例,其中backend表示采用rpc瞬态信息,不保存到数据库;broker表示连接RabbitMQ URL
app = Celery(‘tasks‘,backend=‘rpc://‘,broker=‘pyamqp://guest@localhost//‘) @app.task
def hello():
return "hello celery"

     2.2) 启动celery worker

到tasks.py文件那层目录,执行以下命令:

celery -A tasks worker --loglevel=info

启动输出信息如下:

E:\workdir\test_pro>celery -A tasks worker --loglevel=info
[2017-05-10 18:26:18,298: WARNING/MainProcess] c:\python27\lib\site-packages\celery\apps\worker.py:161: CDeprecationWarnin
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@507B9D97E083 v3.1.25 (Cipater)
---- **** -----
--- * *** * -- Windows-7-6.1.7601-SP1
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x35fc240
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: rpc://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery [tasks]
. tasks.hello [2017-05-10 18:26:18,433: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2017-05-10 18:26:18,483: INFO/MainProcess] mingle: searching for neighbors
[2017-05-10 18:26:19,497: INFO/MainProcess] mingle: all alone
[2017-05-10 18:26:19,523: WARNING/MainProcess] celery@507B9D97E083 ready.

2.3) 测试结果

另起一个终端,还是到tasks.py那层目录,进入python命令行:

这时celery worker会有提示信息:

到此为止,celery入门就介绍到这里了,下一节介绍如何在django中使用celery。

最新文章

  1. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
  2. linux下定时重启tomcat
  3. 【转】unity开发android游戏(一)搭建Unity安卓开发环境
  4. Linux下查看进程和线程
  5. Get current time and date on Android
  6. [git] git 分支管理和工作流程
  7. JMX操作ActiveMQ(2)
  8. OpenVPN server端配置文件详细说明(转)
  9. JavaScript onclick传递对象参数(easyui传递一行数据时)错误:uncaught SyntaxError: Unexpected identifier
  10. idea下导入Tomcat源码
  11. Windows Server 2012 NAT端口转发
  12. iOS中Date和NString的相互转换
  13. UWP 五星评价(不跳转到龟速商店)
  14. typescript 的一种引入文件的方式 Triple-Slash Directives
  15. Qtcreator中常用快捷键总结
  16. 《Java入门第二季》第四章 多态
  17. convertTo
  18. Xamarin.Forms学习之Page Navigation(一)
  19. [原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析
  20. NEU 1685: All Pair Shortest Path

热门文章

  1. ROS Learning-025 (提高篇-003 A Mobile Base-01) 控制移动平台
  2. 8.8 Deep Learning Software
  3. Vue属性绑定
  4. 多层mvc,thikphp进阶
  5. SQL事务对并发处理的支持
  6. 从100PV到1亿级PV网站架构演变(转)
  7. P2117 小Z的矩阵
  8. 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond
  9. 统计元音(stringstream的-应用)
  10. 《Professional JavaScript for Web Developers》day03