1
+ import os
1
2
import socket
2
3
from itertools import chain , imap
3
4
from redis .exceptions import (
@@ -163,6 +164,7 @@ class Connection(object):
163
164
def __init__ (self , host = 'localhost' , port = 6379 , db = 0 , password = None ,
164
165
socket_timeout = None , encoding = 'utf-8' ,
165
166
encoding_errors = 'strict' , parser_class = DefaultParser ):
167
+ self .pid = os .getpid ()
166
168
self .host = host
167
169
self .port = port
168
170
self .db = db
@@ -284,6 +286,7 @@ class UnixDomainSocketConnection(Connection):
284
286
def __init__ (self , path = '' , db = 0 , password = None ,
285
287
socket_timeout = None , encoding = 'utf-8' ,
286
288
encoding_errors = 'strict' , parser_class = DefaultParser ):
289
+ self .pid = os .getpid ()
287
290
self .path = path
288
291
self .db = db
289
292
self .password = password
@@ -316,15 +319,22 @@ class ConnectionPool(object):
316
319
"Generic connection pool"
317
320
def __init__ (self , connection_class = Connection , max_connections = None ,
318
321
** connection_kwargs ):
322
+ self .pid = os .getpid ()
319
323
self .connection_class = connection_class
320
324
self .connection_kwargs = connection_kwargs
321
325
self .max_connections = max_connections or 2 ** 31
322
326
self ._created_connections = 0
323
327
self ._available_connections = []
324
328
self ._in_use_connections = set ()
325
329
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
+
326
335
def get_connection (self , command_name , * keys , ** options ):
327
336
"Get a connection from the pool"
337
+ self ._checkpid ()
328
338
try :
329
339
connection = self ._available_connections .pop ()
330
340
except IndexError :
@@ -341,8 +351,10 @@ def make_connection(self):
341
351
342
352
def release (self , connection ):
343
353
"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 )
346
358
347
359
def disconnect (self ):
348
360
"Disconnects all connections in the pool"
0 commit comments