python操作完redis,需要关闭连接的吧,怎么关闭呢

1人赞 回复

redis-server会关闭空闲超时的连接
redis.conf中可以设置超时时间:
timeout 300

2017.10.21 11:16 回复

如果使用连接池就不需要关闭。

当我们用Redis和StrictRedis创建连接时,其实内部实现并没有主动给我创建一个连接,我们获得的连接是连接池提供的连接,这个连接由连接池管理,所以我们无需关注连接是否需要主动释放的问题。另外连接池有自己的关闭连接的接口,一旦调用该接口,所有连接都将被关闭。

附:

Redis in python, how do you close the connection?

up vote 0 down vote favorite
1
https://github.com/andymccurdy/redis-py

I know in ruby we use the quit() method. I can't find anything here for 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()
python redis
share|improve this question
asked Jul 21 at 22:20

nevermind
555319
 
 
Looking at the source code, StrictRedis doesn't implement close or quit methods. – jonrsharpe Jul 21 at 22:33
 
is it okay that we don't close the connection, I don't think I understand connection to redis ... – nevermind Jul 21 at 22:39
 
@nevermind I see r.client_kill, but to find out, which client to kill, you have to list them by r.client_list(). Checking $ netstat | grep 6379 I saw, the connection got into "closing" state. There is also r.execute_command("QUIT"). But I am still not sure, if it does, what you ask for. – Jan Vlcinsky Jul 21 at 22:44
 
do we need to kill it? can I safely use StrictRedis and not worry about the connection? – nevermind Jul 21 at 23:48
add a comment |
2 Answers 2
active oldest votes
up vote 1 down vote accepted
Just use redis.Redis. It uses a connection pool under the hood, so you don't have to worry about managing at that level.

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.

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.Redis is the way to go in 99.9% of cases.

share|improve this answer
answered Jul 22 at 0:09

SpiritMachine
972411
 
 
add a comment |

up vote 0 down vote
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 disconnect method 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)

share|improve this answer
edited Jul 22 at 7:13

answered Jul 21 at 22:41

sirlark
856615
 
 
If I decide to go with Strict, do I need to worry about the connection? – nevermind Jul 21 at 23:25
---------------------
作者:ysh_ysh
来源:CSDN
原文:https://blog.csdn.net/woshikalz/article/details/40130555
版权声明:本文为博主原创文章,转载请附上博文链接!

手动关闭

r.connection_pool.disconnect()

关于python语言使用redis时,连接是否需要关闭的问题的更多相关文章

  1. python在使用redis时zadd错误

    最近在看<redis实战>,在写zadd时报错 Traceback (most recent call last): File "<stdin>", lin ...

  2. Python语言编写脚本时,对日期控件的处理方式

    对日期控件,日期控件的输入控一般是不能手动输入的:把readonly属性去掉就好 其实很简单,我们不去搞时间日期空间,我们把它当成一个普通的input框处理就好了! 但是,很多此类型input框都是禁 ...

  3. 安装redis,以及python如何引用redis

    下载 cd /usr/local/src/ wget http://download.redis.io/releases/redis-2.8.17.tar.gz 解压 tar -zxvf redis- ...

  4. python 关闭redis的连接

    在python语言中使用redis时,没有找到对应的关闭的方法 try: self.redisconn = StrictRedisCluster(startup_nodes=self.redisNod ...

  5. python redis之连接池的原理

    python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...

  6. 在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: ‘文件路径’

    如题,在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: '文件路径',在查阅了大量资料后也得到了一些解决方案,但是这些解决方案 ...

  7. Python/dotNET Redis服务连接客户端调用SET方法的同时获取Redis服务器返回的内容

    在用Python或dotNET redis客户端连接redis服务器的时候,当你调用客户端的SET方法后同时还想得到其返回的字符串,那么需要处理一下. 1. Redis Python redis客户端 ...

  8. 基于Python项目的Redis缓存消耗内存数据简单分析(附详细操作步骤)

    目录 1 准备工作 2 具体实施   1 准备工作 什么是Redis? Redis:一个高性能的key-value数据库.支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使 ...

  9. Python下使用 redis数据库

    初识Rdeis数据库 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zs ...

随机推荐

  1. waiting TTFB 时间优化

    百度百科解释:获取在接收到响应的首字节前花费的毫秒数. 根据chrome浏览器,具体请求链接的这个时间,对反应慢的页面进行优化.

  2. [BZOJ4052][Cerc2013]Magical GCD

    [BZOJ4052][Cerc2013]Magical GCD 试题描述 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12.  求一个连续子序列,使得在所有的连续子序列中,它们 ...

  3. jvm参数设置 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M

    -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 这里有几个问题: 1. 各个参数的含义什么? 2. 为什么有的机器我将- ...

  4. 可以学习相关框架【转:https://testerhome.com/topics/6283】

    https://testerhome.com/topics/6283 单元测试方面(Java): Junit:本来想用我熟悉的testng,但是开发的同学说测试springmvc只能用Junit.所以 ...

  5. unbuntu下安装多个JAVA JDK版本及如何切换

    当前环境已经安装过jdk1.6.0_45安装JDK 1.7.x时,若安装错误,可执行以下步骤:sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-ge ...

  6. msp430入门编程40

    msp430中C语言的软件工程--前后台程序结构

  7. 通过rsync+inotify实现数据的实时备份 【转载】

       在前面的博文中,我讲到过利用rsync实现数据的镜像和备份,但是要实现数据的实时备份,单独靠rsync还不能实现,本文就讲述下如何实现数据的实时备份. 一.rsync的优点与不足  与传统的cp ...

  8. SeaJS项目完整实例【转】

    index.html——主页面. sea.js——SeaJS脚本. init.js——init模块,入口模块,依赖data.jquery.style三个模块.由主页面载入. data.js——data ...

  9. 123. Best Time to Buy and Sell Stock III ~~

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. markdown八条基础语法

    1.空行 答:使用全角打出空格,之后再换行就可以打出空行了 2.标题 答:#表示标题,#表示一级标题,字号最大,一共有六级标题 3.列表 答:- 无序列表,1. 有序列表,注意和文本之间有空格 4.链 ...