@@ -407,6 +407,7 @@ def __init__(self, host=None, port=None, max_pool_size=100,
407407
408408 # cache of existing indexes used by ensure_index ops
409409 self .__index_cache = {}
410+ self .__index_cache_lock = threading .Lock ()
410411 self .__auth_credentials = {}
411412
412413 super (MongoClient , self ).__init__ (** options )
@@ -446,28 +447,36 @@ def _cached(self, dbname, coll, index):
446447 """
447448 cache = self .__index_cache
448449 now = datetime .datetime .utcnow ()
449- return (dbname in cache and
450- coll in cache [dbname ] and
451- index in cache [dbname ][coll ] and
452- now < cache [dbname ][coll ][index ])
450+ self .__index_cache_lock .acquire ()
451+ try :
452+ return (dbname in cache and
453+ coll in cache [dbname ] and
454+ index in cache [dbname ][coll ] and
455+ now < cache [dbname ][coll ][index ])
456+ finally :
457+ self .__index_cache_lock .release ()
453458
454459 def _cache_index (self , database , collection , index , cache_for ):
455460 """Add an index to the index cache for ensure_index operations.
456461 """
457462 now = datetime .datetime .utcnow ()
458463 expire = datetime .timedelta (seconds = cache_for ) + now
459464
460- if database not in self .__index_cache :
461- self .__index_cache [database ] = {}
462- self .__index_cache [database ][collection ] = {}
463- self .__index_cache [database ][collection ][index ] = expire
465+ self .__index_cache_lock .acquire ()
466+ try :
467+ if database not in self .__index_cache :
468+ self .__index_cache [database ] = {}
469+ self .__index_cache [database ][collection ] = {}
470+ self .__index_cache [database ][collection ][index ] = expire
464471
465- elif collection not in self .__index_cache [database ]:
466- self .__index_cache [database ][collection ] = {}
467- self .__index_cache [database ][collection ][index ] = expire
472+ elif collection not in self .__index_cache [database ]:
473+ self .__index_cache [database ][collection ] = {}
474+ self .__index_cache [database ][collection ][index ] = expire
468475
469- else :
470- self .__index_cache [database ][collection ][index ] = expire
476+ else :
477+ self .__index_cache [database ][collection ][index ] = expire
478+ finally :
479+ self .__index_cache_lock .release ()
471480
472481 def _purge_index (self , database_name ,
473482 collection_name = None , index_name = None ):
@@ -477,22 +486,26 @@ def _purge_index(self, database_name,
477486
478487 If `collection_name` is None purge an entire database.
479488 """
480- if not database_name in self .__index_cache :
481- return
489+ self .__index_cache_lock .acquire ()
490+ try :
491+ if not database_name in self .__index_cache :
492+ return
482493
483- if collection_name is None :
484- del self .__index_cache [database_name ]
485- return
494+ if collection_name is None :
495+ del self .__index_cache [database_name ]
496+ return
486497
487- if not collection_name in self .__index_cache [database_name ]:
488- return
498+ if not collection_name in self .__index_cache [database_name ]:
499+ return
489500
490- if index_name is None :
491- del self .__index_cache [database_name ][collection_name ]
492- return
501+ if index_name is None :
502+ del self .__index_cache [database_name ][collection_name ]
503+ return
493504
494- if index_name in self .__index_cache [database_name ][collection_name ]:
495- del self .__index_cache [database_name ][collection_name ][index_name ]
505+ if index_name in self .__index_cache [database_name ][collection_name ]:
506+ del self .__index_cache [database_name ][collection_name ][index_name ]
507+ finally :
508+ self .__index_cache_lock .release ()
496509
497510 def _cache_credentials (self , source , credentials , connect = True ):
498511 """Add credentials to the database authentication cache
0 commit comments