@@ -327,13 +327,16 @@ def lock(self, name, timeout=None, sleep=0.1):
327
327
"""
328
328
return Lock (self , name , timeout = timeout , sleep = sleep )
329
329
330
- def pubsub (self , shard_hint = None ):
330
+ def pubsub (self , shard_hint = None , release_connection = True ):
331
331
"""
332
332
Return a Publish/Subscribe object. With this object, you can
333
333
subscribe to channels and listen for messages that get published to
334
334
them.
335
+
336
+ ``shard_hint`` and ``release_connection`` are passed to the
337
+ :class:`PubSub` constructor.
335
338
"""
336
- return PubSub (self .connection_pool , shard_hint )
339
+ return PubSub (self .connection_pool , shard_hint , release_connection )
337
340
338
341
#### COMMAND EXECUTION AND PROTOCOL PARSING ####
339
342
def execute_command (self , * args , ** options ):
@@ -1364,10 +1367,18 @@ class PubSub(object):
1364
1367
After subscribing to one or more channels, the listen() method will block
1365
1368
until a message arrives on one of the subscribed channels. That message
1366
1369
will be returned and it's safe to start listening again.
1370
+
1371
+ The default is to recycle the connection when parsing a response (usually
1372
+ by calling ``listen()``) indicating that this connection is not subscribed
1373
+ to any channel anymore. This can be changed by passing ``False`` to
1374
+ ``release_connection``, which will hold on the same connection regardless
1375
+ of its subscriptions.
1367
1376
"""
1368
- def __init__ (self , connection_pool , shard_hint = None ):
1377
+ def __init__ (self , connection_pool , shard_hint = None ,
1378
+ release_connection = True ):
1369
1379
self .connection_pool = connection_pool
1370
1380
self .shard_hint = shard_hint
1381
+ self .release_connection = release_connection
1371
1382
self .connection = None
1372
1383
self .channels = set ()
1373
1384
self .patterns = set ()
@@ -1431,7 +1442,7 @@ def parse_response(self):
1431
1442
self .subscription_count = response [2 ]
1432
1443
# if we've just unsubscribed from the remaining channels,
1433
1444
# release the connection back to the pool
1434
- if not self .subscription_count :
1445
+ if self . release_connection and not self .subscription_count :
1435
1446
self .reset ()
1436
1447
return response
1437
1448
0 commit comments