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()

最新文章

  1. 封装的ajax
  2. HTML5 学习笔记(四)——canvas绘图、WebGL、SVG
  3. python单元测试unittest
  4. Spring入门
  5. [LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点
  6. iOS开发多线程篇—NSOperation基本操作
  7. turing 项目引用
  8. Java Web开发中的名词解释
  9. windows10UWP:Segoe MDL2 图标指南
  10. HTML&CSS基础学习笔记1.2-HTML的全局属性?
  11. c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串
  12. SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数
  13. 每个黑客黑客的目标是让目标系统做你不想做的事情。 一个主要的例子是显示敏感文件,如/ etc / passwd和/ etc / shadow(存储用户的用户名和加密密码)。一旦这些文件在他或她的手中,就可以使用“字典“攻击的密码。 或者,他们可以使您的系统FTP受感染的文件,并运行它,这可能是坏或坏。为了做到这一点,他们需要得到一个“可信”的程序来执行他们指定的命令。通常,这是通过“缓冲区
  14. [进程管理]Linux进程状态解析之R、S、D
  15. poj2566尺取变形
  16. html&css学习一
  17. Cherrypy文件上传非ASCII文件名乱码问题解决
  18. Log4Net配置日志
  19. 339A
  20. java基础---->多线程之Runnable(一)

热门文章

  1. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
  2. 关于srand()rand()的用法
  3. THUWC2018 暴力+爆炸记
  4. C. Day at the Beach---cf559
  5. Java模拟斗地主(实现大小排序)
  6. flash update
  7. top命令行含义解析
  8. C#编程语言及.NET 平台快速入门指南
  9. 利用百度地图Android sdk高仿微信发送位置功能
  10. SpringMVC+Hibernate+Junit4+json基本框架近乎0配置