關於python語言使用redis時,連接是否需要關閉的問題


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM