您的位置:首页 > 其它

celery(一)分布式任务调度模块简介及运行环境

2017-12-26 13:25 816 查看

Celery是Python开发的分布式任务调度模块。

Celery本身不含消息服务,它使用第三方消息服务来传递任务。

django下有个分支Django-Celery,可以结合django来实现任务的编排,计划。

我既然已经选择了使用django作为用户界面,那么后台数据抓取,自动处理,要有一个分布式异步任务调度来处理。一般都选的Celery,而且方便结合django管理。

官方文档中关于版本的介绍,下一个版本Celery 5.X,就不支持python2.7了,我们就直接用最新版吧:

python 3.6.3  celery 4.1.0 celery-with-redis

OS:windows10 home x64

IDE:Pycharm

 

1.1 Brokers

brokers 中文意思为中间人,在这里就是指任务队列本身,Celery 扮演生产者和消费者的角色,brokers 就是生产者和消费者存放/拿取产品的地方(队列)

常见的 brokers 有 rabbitmq、redis、Zookeeper 等

1.2 Result Stores / backend

顾名思义就是结果储存的地方,队列中的任务运行完后的结果或者状态需要被任务发送者知道,那么就需要一个地方储存这些结果,就是 Result Stores 了

常见的 backend 有 redis、Memcached 甚至常用的数据都可以。

1.3 Workers

就是 Celery 中的工作者,类似与生产/消费模型中的消费者,其从队列中取出任务并执行

1.4 Tasks

就是我们想在队列中进行的任务咯,一般由用户、触发器或其他操作将任务入队,然后交由 workers 进行处理。

理解以上概念后我们就可以快速实现一个队列的操作:

 

Celery version 4.0 runs on,

  • Python (2.7, 3.4, 3.5)
  • PyPy (5.4, 5.5)

This is the last version to support Python 2.7, and from the next version (Celery 5.x) Python 3.5 or newer is required.

If you’re running an older version of Python, you need to be running an older version of Celery:

  • Python 2.6: Celery series 3.1 or earlier.
  • Python 2.5: Celery series 3.0 or earlier.
  • Python 2.4 was Celery series 2.2 or earlier.

消息服务

Name Status Monitoring Remote Control
RabbitMQ Stable Yes Yes
Redis Stable Yes Yes
Amazon SQS Stable No No
Zookeeper Experimental No No

 

 

 

 

看来稳定的只有 RabbitMQ 和Redis。linux一定没问题。

windows下我想装RabbitMQ的时候,又要我先装其他的软件,比较烦这些乱装的东西,那就去看Redis,没有windows版,不过有个windows另外开发的。

只有64位版,32位版的系统就不要想了,去看看RabbitMQ吧。

https://github.com/MicrosoftArchive/redis

直接去releases下载zip,解压有这些东西:

redis-benchmark.exe         #基准测试
redis-check-aof.exe         # aof
redis-cli.exe               # 客户端
redis-server.exe            # 服务器
redis.windows.conf          # 配置文件

默认的也不用改什么,双击redis-server.exe,启动redis

[9920] 26 Dec 11:55:13.873 # Warning: no config file specified, using the default config. In order to specify a config file use E:\Redis-x64-3.2.100\redis-server.exe /path/to/redis.conf
_._
_.-``__ ''-._
_.-``    `.  `_.  ''-._           Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```.  ```\/    _.,_ ''-._
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 9920
`-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |           http://redis.io
`-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
`-._    `-.__.-'    _.-'
`-._        _.-'
`-.__.-'

[9920] 26 Dec 11:55:13.883 # Server started, Redis version 3.2.100
[9920] 26 Dec 11:55:13.883 * The server is now ready to accept connections on port 6379

双击redis-cli.exe  

进行一下连接测试:命令有set get 

127.0.0.1:6379> set name jack
OK
127.0.0.1:6379> get name
"jack"
127.0.0.1:6379>

set返回OK

get返回刚才设置的值

一切正常。

这都测试用着方便,你要部署生产环境,现在推荐的是docker

创建tasks.py

from celery import Celery
#地址最后的/0是指使用数据库0,生产环境这个redis可能给多个服务提供服务。
app = Celery('tasks', broker='redis://127.0.0.1:6379/0')
@app.task
def add(x, y):
return x + y

启动worker

在tasks.py目录下运行

celery -A tasks worker --loglevel=info

 

调用任务:

创建run.py,这里线使用delay方法。

from tasks import add
add.delay(4, 4)

  你会在celery的worker窗口,看到运行的结果

[2017-12-26 13:00:55,114: INFO/MainProcess] Received task: tasks.add[997ac417-0643-4bfa-9419-462f33331c6d]
[2017-12-26 13:00:55,125: INFO/MainProcess] Task tasks.add[997ac417-0643-4bfa-9419-462f33331c6d] succeeded in 0.014999999984866008s: 8

至此,celery运行环境已经配置演示完成。

包括python运行,使用的IDE

包括redis 服务,使用windows版redis绿色运行

包括worker,使用命令行celery运行。

下面要研究celery返回值的处理。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: