Celery 简介¶
Introduction to Celery
什么是任务队列?¶
What's a Task Queue?
任务队列用于在不同的线程或机器之间分发工作负载。
任务队列的输入是一种称为任务(task)的工作单元。专门的 worker 进程会持续监控任务队列,以获取新的工作任务来执行。
Celery 通过消息进行通信,通常使用一个消息代理(broker)在客户端与 worker 之间进行协调。要启动一个任务,客户端会向队列中添加一条消息,然后 broker 会将该消息传递给某个 worker。
一个 Celery 系统可以由多个 worker 和 broker 组成,从而实现高可用性(HA)和水平扩展(horizontal scaling)。
Celery 使用 Python 编写,但其协议可以由任何语言实现。除了 Python,还有适用于 Node.js 的 node-celery 和 node-celery-ts,以及一个 PHP 客户端。
也可以通过暴露 HTTP 接口,并让任务发起请求(即 webhook)来实现不同语言之间的互操作。
Task queues are used as a mechanism to distribute work across threads or machines.
A task queue's input is a unit of work called a task. Dedicated worker processes constantly monitor task queues for new work to perform.
Celery communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task the client adds a message to the queue, the broker then delivers that message to a worker.
A Celery system can consist of multiple workers and brokers, giving way to high availability and horizontal scaling.
Celery is written in Python, but the protocol can be implemented in any language. In addition to Python there's node-celery and node-celery-ts for Node.js, and a PHP client.
Language interoperability can also be achieved exposing an HTTP endpoint and having a task that requests it (webhooks).
我需要什么?¶
What do I need?
Celery 需要一个消息传输机制来发送和接收消息。 RabbitMQ 和 Redis 的 broker 传输实现是功能完整的, 此外还有许多其他实验性解决方案可用,包括使用 SQLite 进行本地开发。
Celery 可以运行在单台机器上、多台机器上,甚至可以跨数据中心运行。
Celery requires a message transport to send and receive messages. The RabbitMQ and Redis broker transports are feature complete, but there's also support for a myriad of other experimental solutions, including using SQLite for local development.
Celery can run on a single machine, on multiple machines, or even across data centers.
开始使用¶
Get Started
如果这是你第一次使用 Celery,或者你在 3.1 版本之后没有持续关注其发展(即从更早版本迁移过来), 那么你应该首先阅读我们的入门教程:
If this is the first time you're trying to use Celery, or if you haven't kept up with development in the 3.1 version and are coming from previous versions, then you should read our getting started tutorials:
Celery 是……¶
Celery is…
功能¶
Features
框架集成¶
Framework Integration
Celery 易于与各类 Web 框架集成,其中一些框架甚至提供了专门的集成包:
不需要 |
|
关于 Django ,请参考 使用 Django 的第一步。
这些集成包并不是必需的, 但它们可以使开发过程更简单, 有时还提供了重要的扩展钩子, 例如在 fork(2) 时正确关闭数据库连接。
Celery is easy to integrate with web frameworks, some of them even have integration packages:
not needed |
|
For Django see 使用 Django 的第一步.
The integration packages aren't strictly necessary, but they can make development easier, and sometimes they add important hooks like closing database connections at fork(2).
快速跳转¶
Quick Jump
安装¶
Installation
您可以通过 Python 包索引 (PyPI) 或从源代码安装 Celery。
使用 pip 安装:
You can install Celery either via the Python Package Index (PyPI) or from source.
To install using pip:
$ pip install -U Celery
软件包¶
Bundles
Celery 还定义了一组捆绑包(bundles), 可用于安装 Celery 及特定功能所需的依赖项。
你可以在 requirements 文件中指定这些捆绑包, 或在 pip 命令行中通过使用中括号的方式指定。 若需指定多个捆绑包,可使用逗号分隔。
$ pip install "celery[librabbitmq]"
$ pip install "celery[librabbitmq,redis,auth,msgpack]"
可用的捆绑包包括:
Celery also defines a group of bundles that can be used to install Celery and the dependencies for a given feature.
You can specify these in your requirements or on the pip command-line by using brackets. Multiple bundles can be specified by separating them by commas.
$ pip install "celery[librabbitmq]"
$ pip install "celery[librabbitmq,redis,auth,msgpack]"
The following bundles are available:
序列化器¶
Serializers
celery[auth]
:用于启用
auth
安全序列化器。celery[msgpack]
:用于启用 msgpack 序列化器。
celery[yaml]
:用于启用 yaml 序列化器。
celery[auth]
:for using the
auth
security serializer.celery[msgpack]
:for using the msgpack serializer.
celery[yaml]
:for using the yaml serializer.
并发¶
Concurrency
celery[eventlet]
:celery[gevent]
:用于启用 https://pypi.org/project/gevent/ 池。
celery[eventlet]
:for using the https://pypi.org/project/eventlet/ pool.
celery[gevent]
:for using the https://pypi.org/project/gevent/ pool.
传输和后端¶
Transports and Backends
celery[librabbitmq]
:用于启用 librabbitmq C 语言库。
celery[redis]
:用于将 Redis 用作消息传输或结果存储后端。
celery[sqs]
:用于将 Amazon SQS 用作消息传输(实验性)。
celery[tblib]
:用于启用
task_remote_tracebacks
功能。celery[memcache]
:使用 Memcached 作为结果存储后端(基于 https://pypi.org/project/pylibmc/)。
celery[pymemcache]
:使用 Memcached 作为结果存储后端(纯 Python 实现)。
celery[cassandra]
:使用 Apache Cassandra / Astra DB 作为结果存储后端,基于 DataStax 驱动。
celery[couchbase]
:使用 Couchbase 作为结果存储后端。
celery[arangodb]
:使用 ArangoDB 作为结果存储后端。
celery[elasticsearch]
:使用 Elasticsearch 作为结果存储后端。
celery[riak]
:使用 Riak 作为结果存储后端。
celery[dynamodb]
:使用 AWS DynamoDB 作为结果存储后端。
celery[zookeeper]
:使用 Zookeeper 作为消息传输。
celery[sqlalchemy]
:使用 SQLAlchemy 作为结果存储后端(官方支持)。
celery[pyro]
:使用 Pyro4 作为消息传输(实验性)。
celery[slmq]
:使用 SoftLayer Message Queue 作为消息传输(实验性)。
celery[consul]
:使用 Consul.io Key/Value 存储作为消息传输或结果存储后端(实验性)。
celery[django]
:指定用于支持 Django 的最低版本。
通常不推荐在 requirements 中使用该选项, 这里仅作参考。
celery[gcs]
:使用 Google Cloud Storage 作为结果存储后端(实验性)。
celery[gcpubsub]
:使用 Google Cloud Pub/Sub 作为消息传输(实验性)。
celery[librabbitmq]
:for using the librabbitmq C library.
celery[redis]
:for using Redis as a message transport or as a result backend.
celery[sqs]
:for using Amazon SQS as a message transport (experimental).
celery[tblib]
:for using the
task_remote_tracebacks
feature.celery[memcache]
:for using Memcached as a result backend (using https://pypi.org/project/pylibmc/)
celery[pymemcache]
:for using Memcached as a result backend (pure-Python implementation).
celery[cassandra]
:for using Apache Cassandra/Astra DB as a result backend with DataStax driver.
celery[couchbase]
:for using Couchbase as a result backend.
celery[arangodb]
:for using ArangoDB as a result backend.
celery[elasticsearch]
:for using Elasticsearch as a result backend.
celery[riak]
:for using Riak as a result backend.
celery[dynamodb]
:for using AWS DynamoDB as a result backend.
celery[zookeeper]
:for using Zookeeper as a message transport.
celery[sqlalchemy]
:for using SQLAlchemy as a result backend (supported).
celery[pyro]
:for using the Pyro4 message transport (experimental).
celery[slmq]
:for using the SoftLayer Message Queue transport (experimental).
celery[consul]
:for using the Consul.io Key/Value store as a message transport or result backend (experimental).
celery[django]
:specifies the lowest version possible for Django support.
You should probably not use this in your requirements, it's here for informational purposes only.
celery[gcs]
:for using the Google Cloud Storage as a result backend (experimental).
celery[gcpubsub]
:for using the Google Cloud Pub/Sub as a message transport (experimental)..
从源代码下载并安装¶
Downloading and installing from source
可以从 PyPI 下载 Celery 的最新版本:
https://pypi.org/project/celery/
你可以通过以下步骤进行安装:
$ tar xvfz celery-0.0.0.tar.gz
$ cd celery-0.0.0
$ python setup.py build
# python setup.py install
如果你未在虚拟环境(virtualenv)中, 最后一条命令需要以特权用户身份执行。
Download the latest version of Celery from PyPI:
https://pypi.org/project/celery/
You can install it by doing the following,:
$ tar xvfz celery-0.0.0.tar.gz
$ cd celery-0.0.0
$ python setup.py build
# python setup.py install
The last command must be executed as a privileged user if you aren't currently using a virtualenv.
使用开发版本¶
Using the development version
使用 pip¶
With pip
Celery 的开发版还需要依赖以下项目的开发版: https://pypi.org/project/kombu/ 、 https://pypi.org/project/amqp/ 、 https://pypi.org/project/billiard/ 和 https://pypi.org/project/vine/ 。
可以使用以下 pip 命令安装它们的最新快照版:
$ pip install https://github.com/celery/celery/zipball/main#egg=celery
$ pip install https://github.com/celery/billiard/zipball/main#egg=billiard
$ pip install https://github.com/celery/py-amqp/zipball/main#egg=amqp
$ pip install https://github.com/celery/kombu/zipball/main#egg=kombu
$ pip install https://github.com/celery/vine/zipball/main#egg=vine
The Celery development version also requires the development versions of https://pypi.org/project/kombu/, https://pypi.org/project/amqp/, https://pypi.org/project/billiard/, and https://pypi.org/project/vine/.
You can install the latest snapshot of these using the following pip commands:
$ pip install https://github.com/celery/celery/zipball/main#egg=celery
$ pip install https://github.com/celery/billiard/zipball/main#egg=billiard
$ pip install https://github.com/celery/py-amqp/zipball/main#egg=amqp
$ pip install https://github.com/celery/kombu/zipball/main#egg=kombu
$ pip install https://github.com/celery/vine/zipball/main#egg=vine
使用 git¶
With git
详情请参考 贡献指南。
Please see the Contributing section.