Skip to content

Commit

Permalink
Merge pull request #32 from laixintao/ch5
Browse files Browse the repository at this point in the history
Ch5 WIP
  • Loading branch information
laixintao committed Jul 22, 2018
2 parents 3de180f + 289a71c commit 5520d49
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 24 deletions.
6 changes: 6 additions & 0 deletions chapter5/01_Introduction.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
介绍
====

分布式计算基本的思想是将一个大任务分散成几个小任务,交给分布式网络中的计算机去完成。在分布式计算的环境中,必须保证网络中的计算机的可用性(避免网络延迟、非预知的崩溃等)。所以就需要可以可持续的监控架构。

采用这种技术产生的根本问题是对各种类型的流量(数据,任务,命令等)进行适当的分配。此外,还有一个分布式系统基础特征产生的问题:网络由不同操作系统的计算机组成,很多互不兼容。实际上,随着时间的推移,为了使用分布式环境中的不同资源,已经可以识别不同的计算模型。他们的目标是提供一个框架,为一个分布式应用的不同处理器之间描述合作方法。

我们可以说,基本上各种模型的不同点是分布式系统能提供的容量的大小。最常用的模型是服务器/客户端模型。它可以让不同的进程运行在不同的计算机上,通过交换教习进行实时的合作。相对于前一个模型有很大的提高,前一个模型要求将所有的文件分发到所有的机器上,从而进行离线的计算。客户端/服务器模型典型的实现是通过远程的程序调用(这是本地调用的扩展),或者通过分布式对象的程序(面向对象的中间件)。本章将介绍一些Python提供的类似的计算架构。然后我们会介绍一些用OO的方法或远程调用实现了分布式架构的库,例如 Celery,SCOOP, Pyro4 和 RPyC,也有一些使用了不同方法的库,例如 PyCSP 和Disco,Python版本的 MapReduce 算法。
133 changes: 133 additions & 0 deletions chapter5/02_Using_Celery_to_distribute_tasks.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,135 @@
使用Celery实现分布式任务
========================

Celery 是一个 Python 框架,用来管理分布式任务的,遵循面向对象的中间件方法。它的主要 feature 是可以将许多小任务分不到一个大型的计算集群中,最后将任务的结果收集起来,组成整体的解决方案。

要使用 Celery,我们需要下面几种组件:

- Celery 模块(废话!!)
- 一个消息代理(Message Broker)。这是一个独立于 Celery 的一个中间件,用来和分布式的 worker 收发消息。它会处理在通讯网络中的信息交换。这种中间件的消息发送方案不再是点对点的,而是面向消息的方式,最终名的是发布者/订阅者的范式。

.. image:: ../images/celery-diagram.png

Celery 支持很多消息代理,最有名的是 RebbitMQ 和 Redis。

|how|
-----

安装 Celery,我们需要 `pip` 包管理器。在命令行通过以下命令安装: ::

pip install celery

之后,我们必须安装消息代理。在本书中,我们使用 RabbitMQ 来做消息代理,这是一个面向消息的中间件(也叫做代理人消息),它实现了 **高级消息队列协议(AMQP)** 。RabbitMQ 服务端使用 Erlang 写的,基于 **Open Telecom Platform(OPT)** 框架来管理集群和故障转移。安装 RabbitMQ 的步骤如下:

1. 下载安装 Erlang:http://www.erlang.org/download.html
2. 下载并运行 RabbitMQ 安装器:http://www.rabbitmq.com/download.html

下载完成后,安装好 RabbitMQ 然后用默认配置运行即可。

最后,我们会安装 Flower( http://flower.readthedocs.org/ ),这是一个基于 web 的任务监控和管理工具(进行中的任务,任务细节和图表、状态等)。安装方法和 Celery 一样,在命令行输入: ::

pip install -U flower

然后,我们可以验证一下 Celery 的安装,在命令行输入: ::

celery --version

会出现(译者使用的版本比原书高): ::
4.2.1 (windowlicker)

Celery 的用法非常简单,和显示的一样: ::
Usage: celery <command> [options]

显示的配置如下: ::

➜ [parallel-test] python-parallel-programming-cookbook-cn git:(ch5) ✗ celery
usage: celery <command> [options]

Show help screen and exit.

positional arguments:
args

optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit

Global Options:
-A APP, --app APP
-b BROKER, --broker BROKER
--result-backend RESULT_BACKEND
--loader LOADER
--config CONFIG
--workdir WORKDIR
--no-color, -C
--quiet, -q

---- -- - - ---- Commands- -------------- --- ------------

+ Main:
| celery worker
| celery events
| celery beat
| celery shell
| celery multi
| celery amqp

+ Remote Control:
| celery status

| celery inspect --help
| celery inspect active
| celery inspect active_queues
| celery inspect clock
| celery inspect conf [include_defaults=False]
| celery inspect memdump [n_samples=10]
| celery inspect memsample
| celery inspect objgraph [object_type=Request] [num=200 [max_depth=10]]
| celery inspect ping
| celery inspect query_task [id1 [id2 [... [idN]]]]
| celery inspect registered [attr1 [attr2 [... [attrN]]]]
| celery inspect report
| celery inspect reserved
| celery inspect revoked
| celery inspect scheduled
| celery inspect stats

| celery control --help
| celery control add_consumer <queue> [exchange [type [routing_key]]]
| celery control autoscale [max [min]]
| celery control cancel_consumer <queue>
| celery control disable_events
| celery control election
| celery control enable_events
| celery control heartbeat
| celery control pool_grow [N=1]
| celery control pool_restart
| celery control pool_shrink [N=1]
| celery control rate_limit <task_name> <rate_limit (e.g., 5/s | 5/m | 5/h)>
| celery control revoke [id1 [id2 [... [idN]]]]
| celery control shutdown
| celery control terminate <signal> [id1 [id2 [... [idN]]]]
| celery control time_limit <task_name> <soft_secs> [hard_secs]

+ Utils:
| celery purge
| celery list
| celery call
| celery result
| celery migrate
| celery graph
| celery upgrade

+ Debugging:
| celery report
| celery logtool
---- -- - - --------- -- - -------------- --- ------------

Type 'celery <command> --help' for help using a specific command.

|more|
------

有关 Celery 的更多细节,可以访问官方主页: http://www.celeryproject.org/
Binary file added images/celery-diagram.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions plan
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
4 34
5 48
6 57

26 changes: 2 additions & 24 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,2 @@
alabaster==0.7.10
arrow==0.10.0
Babel==2.5.0
certifi==2017.7.27.1
chardet==3.0.4
click==6.7
docutils==0.14
git-ext==0.3.5
idna==2.6
imagesize==0.7.1
Jinja2==2.9.6
MarkupSafe==1.0
Pygments==2.2.0
python-dateutil==2.6.1
pytz==2017.2
PyYAML==3.12
requests==2.18.4
six==1.10.0
snowballstemmer==1.2.1
Sphinx==1.6.3
sphinx-rtd-theme==0.2.4
sphinxcontrib-websupport==1.0.1
typing==3.6.2
urllib3==1.22
sphinx_rtd_theme
sphinx

0 comments on commit 5520d49

Please sign in to comment.