Creating and managing multiple connections in Redis Python

允我心安 提交于 2019-12-03 04:04:06

You really shouldn't create connections like that. Let me quote the redis-py documentation.

Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool. You can override this behavior and use an existing connection pool by passing an already created connection pool instance to the connection_pool argument of the Redis class. You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed.

>>> pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
>>> r = redis.StrictRedis(connection_pool=pool)

You cannot specify a single connection to be used with the library. Each Redis instance will have its own connection pool. When execute_command() is called, it will pop a connection from a the pool(or open a new one) and use that connection. If you only want your client to have max one connection at a time, set max_connections to 1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!