python - celery 、Redis 和连接池

标签 python flask redis celery connection-pooling

我最近在使用 Celery 和 Redis 时遇到了问题:我使用了过多的连接到我的云 Redis 帐户。

我现在似乎已经通过更好的设置和更大的 Redis 计划解决了这个问题。

但是,它引导我对 Redis 和 Celery 如何协同工作进行了一些实验,并且有一些我不明白的东西:由 Python Redis 模块创建的 Redis ConnectionPool 的数量。

我将 Celery 配置为使用 Redis 作为代理和结果后端。

这是我的 celery 配置:

CELERY_TIMEZONE = 'Europe/Paris'
CELERY_BROKER_URL = REDIS_URL
CELERY_RESULT_BACKEND = REDIS_URL
CELERY_TASK_SERIALIZER = 'pickle'
CELERY_SEND_EVENTS = False
CELERY_IMPORTS = ('task1', 'task2')
CELERY_ACCEPT_CONTENT = ['pickle']
CELERY_REDIS_MAX_CONNECTIONS = 2
BROKER_POOL_LIMIT = 1
BROKER_HEARTBEAT = None
BROKER_TRANSPORT_OPTIONS = {
   'max_connections': 30
}

我使用以下命令运行 C​​elery:

celery worker --without-gossip --without-mingle --without-heartbeat --concurrency=1 --app=backZest.celery

Celery 启动后,我的 Redis 实例已经有 10 个连接:这正常吗?

我检查了创建了多少 redis ConnectionPool :为此,我刚刚修改了 Python Redis 模块 connection.py 文件,在ConnectionPool 类的 __init__ 方法:

import sys
print "ConnectionPool instance %s with %s max_connections" % (id(self), self.max_connections)
sys.stdout.flush()

如您所见,创建了许多 (8) 个池,它们都具有相同的 max_connections,30,这是我在 BROKER_TRANSPORT_OPTIONS 中的设置:

ConnectionPool instance 4412957392 with 30 max_connections
ConnectionPool instance 4412959888 with 30 max_connections
ConnectionPool instance 4420369616 with 30 max_connections
ConnectionPool instance 4420370320 with 30 max_connections
ConnectionPool instance 4420367056 with 30 max_connections
ConnectionPool instance 4420491792 with 30 max_connections
ConnectionPool instance 4422318224 with 30 max_connections
ConnectionPool instance 4422319504 with 30 max_connections

我不明白为什么会有这么多。我想控制与我的 Redis 的连接数。我可以控制每个 ConnectionPool 的 max_connections 数量,但如果我不控制创建的 ConnectionPool 数量,这不是有点没用吗?

非常感谢您的帮助!

最佳答案

是的,这是正常的。 Pool 预先创建连接,以便在您需要时准备就绪。建立连接可能会花费很多时间,因此 pool 可以帮助您节省时间。根据需要使用 max_connections 限制池。不用担心池会提前打开连接。

关于python - celery 、Redis 和连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35654308/

相关文章:

python - 为什么我的 Tornado/Flask 服务器在用请求敲打它时会在 Windows 上阻塞并死机?

Python Flask - request.json 返回 None 类型而不是 json 字典

python - 如何按位置实时将推文绘制到 map 上,例如传单

android - 使用 Redis 服务器将数据从 android 发布到 rails

django - 带有随 secret 码的Redis AUTH

python - 无法从 QTableWidget 中选择整行数据

python - 如何在 python 函数中显式指示命名参数的默认值的使用?

python - ManyToMany 上的 django ORM AND 查找

node.js - Redis "Promiseable"在 Node 中吗?

带有 Selenium 的 Python "element is not attached to the page document"