python中的Redis,如何关闭连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24875806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:23:59  来源:igfitidea点击:

Redis in python, how do you close the connection?

pythonredis

提问by

https://github.com/andymccurdy/redis-py

https://github.com/andymccurdy/redis-py

I know in ruby we use the quit() method. I can't find anything here for python

我知道在 ruby​​ 中我们使用了 quit() 方法。我在这里找不到任何关于 python 的东西

python:

Python:

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
print r.get('foo')
#r.close() doesn't work

ruby

红宝石

require "redis"
redis = Redis.new
redis.set("mykey", "hello world")
puts redis.get("mykey")
redis.quit()

采纳答案by manadart

Just use redis.Redis. It uses a connection pool under the hood, so you don't have to worry about managing at that level.

只需使用redis.Redis. 它在后台使用连接池,因此您不必担心在该级别进行管理。

If you absolutely have to use a low level connection, you need to do the response handling that is normally done for you by redis.Redis.

如果您绝对必须使用低级连接,则需要执行通常由redis.Redis.

Here's an example of executing a single command using the low level connection:

以下是使用低级连接执行单个命令的示例:

def execute_low_level(command, *args, **kwargs):
    connection = redis.Connection(**kwargs)
    try:
        connection.connect()
        connection.send_command(command, *args)

        response = connection.read_response()
        if command in redis.Redis.RESPONSE_CALLBACKS:
            return redis.Redis.RESPONSE_CALLBACKS[command](response)
        return response

    finally:
        del connection

Example usage:

用法示例:

response = execute_low_level(
        'HGET', 'redis:key', 'hash:key', host='localhost', port=6379)

But as I said before, redis.Redisis the way to go in 99.9% of cases.

但正如我之前所说,redis.Redis在 99.9% 的情况下都是可行的。

回答by sirlark

StrictRedis doesn't implement connection semantics itself, instead it uses a connection pool, which is available as a property of a StrictRedis instance: S.connection_pool. The connection_pool object has a disconnectmethod to force an immediate disconnect of all connections in the pool if necessary, however when your StrictRedis object goes out of scope, the individual connections in the pool all clean themselves up without your intervention (see redis/connection.py:392-396)

StrictRedis没有实现连接语义本身,而是它使用一个连接池,这是作为一个StrictRedis实例的属性:S.connection_pool。connection_pool 对象有一个disconnect方法可以在必要时强制立即断开池中的所有连接,但是当您的 StrictRedis 对象超出范围时,池中的各个连接都会在没有您干预的情况下自行清理(请参阅 redis/connection.py :392-396)

回答by shangliuyan

you dont need worry about it when you use ConnectionPool.look at the source code:

使用ConnectionPool时无需担心。查看源代码:

def execute_command(self, *args, **options):
    "Execute a command and return a parsed response"
    pool = self.connection_pool
    command_name = args[0]
    connection = pool.get_connection(command_name, **options)
    try: 
        connection.send_command(*args)
        return self.parse_response(connection, command_name, **options)
    except (ConnectionError, TimeoutError) as e:
        connection.disconnect()
        if not connection.retry_on_timeout and isinstance(e, TimeoutError):
            raise
        connection.send_command(*args)
        return self.parse_response(connection, command_name, **options)
    finally:
        pool.release(connection)

finally,every connection will release to the pool no matter what you do, and it will assign to other client.

最后,无论您做什么,每个连接都会释放到池中,并将分配给其他客户端。

回答by Lynn Han

Use Redis connection pool. You don't need to explicitly close it.

使用Redis连接池。您不需要明确关闭它。

import redis

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

And can improve efficiency.

并且可以提高效率。