Changed how the connection identity key is made
Uses the current thread identity as well as the process idenity to form the key. Fixes #151
This commit is contained in:
		| @@ -1,5 +1,6 @@ | |||||||
| from pymongo import Connection | from pymongo import Connection | ||||||
| import multiprocessing | import multiprocessing | ||||||
|  | import threading | ||||||
|  |  | ||||||
| __all__ = ['ConnectionError', 'connect'] | __all__ = ['ConnectionError', 'connect'] | ||||||
|  |  | ||||||
| @@ -22,6 +23,8 @@ class ConnectionError(Exception): | |||||||
|  |  | ||||||
|  |  | ||||||
| def _get_connection(reconnect=False): | def _get_connection(reconnect=False): | ||||||
|  |     """Handles the connection to the database | ||||||
|  |     """ | ||||||
|     global _connection |     global _connection | ||||||
|     identity = get_identity() |     identity = get_identity() | ||||||
|     # Connect to the database if not already connected |     # Connect to the database if not already connected | ||||||
| @@ -33,6 +36,9 @@ def _get_connection(reconnect=False): | |||||||
|     return _connection[identity] |     return _connection[identity] | ||||||
|  |  | ||||||
| def _get_db(reconnect=False): | def _get_db(reconnect=False): | ||||||
|  |     """Handles database connections and authentication based on the current | ||||||
|  |     identity | ||||||
|  |     """ | ||||||
|     global _db, _connection |     global _db, _connection | ||||||
|     identity = get_identity() |     identity = get_identity() | ||||||
|     # Connect if not already connected |     # Connect if not already connected | ||||||
| @@ -52,8 +58,13 @@ def _get_db(reconnect=False): | |||||||
|     return _db[identity] |     return _db[identity] | ||||||
|  |  | ||||||
| def get_identity(): | def get_identity(): | ||||||
|  |     """Creates an identity key based on the current process and thread | ||||||
|  |     identity. | ||||||
|  |     """ | ||||||
|     identity = multiprocessing.current_process()._identity |     identity = multiprocessing.current_process()._identity | ||||||
|     identity = 0 if not identity else identity[0] |     identity = 0 if not identity else identity[0] | ||||||
|  |  | ||||||
|  |     identity = (identity, threading.current_thread().ident) | ||||||
|     return identity |     return identity | ||||||
|  |  | ||||||
| def connect(db, username=None, password=None, **kwargs): | def connect(db, username=None, password=None, **kwargs): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user