Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4db71b9

Browse files
committedAug 25, 2023
Add close() method to redis.ConnectionPool
1 parent 922bb3f commit 4db71b9

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
 

‎redis/connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,10 @@ def disconnect(self, inuse_connections=True):
11511151
for connection in connections:
11521152
connection.disconnect()
11531153

1154+
def close(self) -> None:
1155+
"""Close the pool, disconnecting all connections"""
1156+
self.disconnect()
1157+
11541158
def set_retry(self, retry: "Retry") -> None:
11551159
self.connection_kwargs.update({"retry": retry})
11561160
for conn in self._available_connections:

‎tests/test_connection_pool.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import time
4+
from contextlib import closing
45
from threading import Thread
56
from unittest import mock
67

@@ -51,6 +52,16 @@ def test_connection_creation(self):
5152
assert isinstance(connection, DummyConnection)
5253
assert connection.kwargs == connection_kwargs
5354

55+
def test_closing(self):
56+
connection_kwargs = {"foo": "bar", "biz": "baz"}
57+
pool = redis.ConnectionPool(
58+
connection_class=DummyConnection,
59+
max_connections=None,
60+
**connection_kwargs,
61+
)
62+
with closing(pool):
63+
pass
64+
5465
def test_multiple_connections(self, master_host):
5566
connection_kwargs = {"host": master_host[0], "port": master_host[1]}
5667
pool = self.get_pool(connection_kwargs=connection_kwargs)

0 commit comments

Comments
 (0)
Please sign in to comment.