Skip to content

Commit 2a3e05c

Browse files
committed
all tests pass now except pub/sub. connection_pool's get_connection now always received the command name for the next command. still need to pass keys.
1 parent c650073 commit 2a3e05c

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

redis/client.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,6 @@ def __init__(self, host='localhost', port=6379,
191191
encoding_errors=errors
192192
)
193193

194-
#### Legacy accessors of connection information ####
195-
def _get_host(self):
196-
return self.connection.host
197-
host = property(_get_host)
198-
199-
def _get_port(self):
200-
return self.connection.port
201-
port = property(_get_port)
202-
203-
def _get_db(self):
204-
return self.connection.db
205-
db = property(_get_db)
206-
207194
def pipeline(self, transaction=True):
208195
"""
209196
Return a new pipeline object that can queue multiple commands for
@@ -230,9 +217,9 @@ def lock(self, name, timeout=None, sleep=0.1):
230217

231218
#### COMMAND EXECUTION AND PROTOCOL PARSING ####
232219
def execute_command(self, *args, **options):
233-
connection = self.connection_pool.get_connection()
220+
command_name = args[0]
221+
connection = self.connection_pool.get_connection(command_name)
234222
try:
235-
command_name = args[0]
236223
subscription_command = command_name in self.SUBSCRIPTION_COMMANDS
237224
if self.subscribed and not subscription_command:
238225
raise PubSubError("Cannot issue commands other than SUBSCRIBE "
@@ -1121,6 +1108,9 @@ def hvals(self, name):
11211108
"Return the list of values within hash ``name``"
11221109
return self.execute_command('HVALS', name)
11231110

1111+
def pubsub(self):
1112+
return PubSub(self.connection_pool)
1113+
11241114

11251115
# channels
11261116
def psubscribe(self, patterns):
@@ -1236,7 +1226,7 @@ def execute_command(self, *args, **options):
12361226
return self
12371227

12381228
def _execute_transaction(self, commands):
1239-
connection = self.connection_pool.get_connection()
1229+
connection = self.connection_pool.get_connection('MULTI')
12401230
try:
12411231
all_cmds = ''.join(starmap(connection.pack_command,
12421232
[args for args, options in commands]))
@@ -1272,7 +1262,7 @@ def _execute_transaction(self, commands):
12721262

12731263
def _execute_pipeline(self, commands):
12741264
# build up all commands into a single request to increase network perf
1275-
connection = self.connection_pool.get_connection()
1265+
connection = self.connection_pool.get_connection('MULTI')
12761266
try:
12771267
all_cmds = ''.join(starmap(connection.pack_command,
12781268
[args for args, options in commands]))

redis/connection.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,25 @@ def __init__(self, connection_class=Connection, **kwargs):
244244
self.connection_class = connection_class
245245
self.kwargs = kwargs
246246
self._connection = None
247+
self._in_use = False
247248

248-
def get_connection(self, *args, **kwargs):
249+
def copy(self):
250+
"Return a new instance of this class with the same parameters"
251+
return self.__class__(self.connection_class, **self.kwargs)
252+
253+
def get_connection(self, command_name, *keys):
249254
"Get a connection from the pool"
255+
if self._in_use:
256+
raise ConnectionError("Connection already in-use")
250257
if not self._connection:
251258
self._connection = self.connection_class(**self.kwargs)
259+
self._in_use = True
252260
return self._connection
253261

254262
def release(self, connection):
255263
"Releases the connection back to the pool"
256-
pass
264+
assert self._connection == connection
265+
self._in_use = False
257266

258267
def disconnect(self):
259268
"Disconnects all connections in the pool"

0 commit comments

Comments
 (0)