Package: mdb-singleton
MongoDB Singletonmdb_singletonis a Python package designed to optimize MongoDB connection management, avoiding unnecessary opening and closing of connections and efficiently utilizing existing connections. The package focuses on mitigating resource wastage while ensuring a scalable and thread-safe approach.InstallationInstall the package using pip:pipinstallmdb_singletonOverviewThe primary goal ofmdb_singletonis to address the challenges associated with MongoDB connection handling in a resource-efficient manner. The package achieves this by generating thread-specific and task-specific singletons, allowing multiple threads or tasks to leverage their dedicated connections concurrently, eliminating bottlenecks.FeaturesThread-Safe Singletons:MongoDBSingletonfacilitates the creation of MongoDB connections unique to each thread, preventing contention and ensuring thread safety.Asyncio (ASGI) Support:MongoDBSingletonAsyncextends the functionality for asynchronous applications, creating dedicated connections for each asyncio task.Resource Optimization:mdb_singletoneliminates unnecessary connection overhead by reusing existing connections within the context of a thread or asyncio task.UsageBasic Usagefrommdb_singletonimportMongoDBSingleton# Create or retrieve a MongoDB connection singleton for the current threadmongo_conn=MongoDBSingleton()client=mongo_conn.client# Your MongoDB operations here...# Avoid closing the connection manually, as the package is designed for reuse.ASGI (Asyncio) Usagefrommdb_singletonimportMongoDBSingletonAsync# Create or retrieve an async MongoDB connection singleton for the current asyncio taskmongo_conn=MongoDBSingletonAsync()client=mongo_conn.client# Your async MongoDB operations here...# Avoid closing the connection manually, as the package is designed for reuse.Connection Closure on Application ExitTo ensure proper closure of all MongoDB connections initiated withMongoDBSingletonandMongoDBSingletonAsyncwhen the program or server terminates, include the following line of code in your startup script, settings.py, or any relevant location:importatexitfrommdb_singletonimportMongoDBSingleton# Register connection closure on application exitatexit.register(MongoDBSingleton.close_all_connections)Manual Closure (Exceptional Cases)In exceptional cases where manual closure is necessary, it should be done using the following syntax:frommdb_singletonimportMongoDBSingletonmongo_conn=MongoDBSingleton()client=mongo_conn.client# Your MongoDB operations here...# Close the MongoDB connection manuallyMongoDBSingleton.close_connection(mongo_conn.key)This method will close the connection and remove the instance from the active connections.Environment Variablemdb_singletonexpects the following environment variable to be set for MongoDB connection:# .env fileMONGO_URI="mongodb+srv://..."Why Usemdb_singleton?Efficient Resource Utilization:Avoids unnecessary opening and closing of connections, optimizing resource usage.Thread and Task Specific:Generates dedicated connections for each thread or asyncio task, preventing contention and bottlenecks.Scalable Design:Scales effectively with multi-threaded and asyncio applications, adapting to the demands of concurrent execution.LicenseCopyright © 2024nivwer.This package is licensed under theMIT License.