1+ import os
12import socket
23from itertools import chain , imap
34from redis .exceptions import (
@@ -163,6 +164,7 @@ class Connection(object):
163164 def __init__ (self , host = 'localhost' , port = 6379 , db = 0 , password = None ,
164165 socket_timeout = None , encoding = 'utf-8' ,
165166 encoding_errors = 'strict' , parser_class = DefaultParser ):
167+ self .pid = os .getpid ()
166168 self .host = host
167169 self .port = port
168170 self .db = db
@@ -284,6 +286,7 @@ class UnixDomainSocketConnection(Connection):
284286 def __init__ (self , path = '' , db = 0 , password = None ,
285287 socket_timeout = None , encoding = 'utf-8' ,
286288 encoding_errors = 'strict' , parser_class = DefaultParser ):
289+ self .pid = os .getpid ()
287290 self .path = path
288291 self .db = db
289292 self .password = password
@@ -316,15 +319,22 @@ class ConnectionPool(object):
316319 "Generic connection pool"
317320 def __init__ (self , connection_class = Connection , max_connections = None ,
318321 ** connection_kwargs ):
322+ self .pid = os .getpid ()
319323 self .connection_class = connection_class
320324 self .connection_kwargs = connection_kwargs
321325 self .max_connections = max_connections or 2 ** 31
322326 self ._created_connections = 0
323327 self ._available_connections = []
324328 self ._in_use_connections = set ()
325329
330+ def _checkpid (self ):
331+ if self .pid != os .getpid ():
332+ self .disconnect ()
333+ self .__init__ (self .connection_class , self .max_connections , ** self .connection_kwargs )
334+
326335 def get_connection (self , command_name , * keys , ** options ):
327336 "Get a connection from the pool"
337+ self ._checkpid ()
328338 try :
329339 connection = self ._available_connections .pop ()
330340 except IndexError :
@@ -341,8 +351,10 @@ def make_connection(self):
341351
342352 def release (self , connection ):
343353 "Releases the connection back to the pool"
344- self ._in_use_connections .remove (connection )
345- self ._available_connections .append (connection )
354+ self ._checkpid ()
355+ if connection .pid == self .pid :
356+ self ._in_use_connections .remove (connection )
357+ self ._available_connections .append (connection )
346358
347359 def disconnect (self ):
348360 "Disconnects all connections in the pool"
0 commit comments