redis
Python 连结redis 需要主动断开链接么?
使用redis 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)
Reference :
https://stackoverflow.com/questions/24875806/redis-in-python-how-do-you-close-the-connection
网友评论