Skip to main content

MongoDB connection management with thread-safe and asyncio-compatible singletons.

Project description

MongoDB Singleton

mdb_singleton is 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.

Installation

Install the package using pip:

pip install mdb_singleton

Overview

The primary goal of mdb_singleton is 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.

Features

  • Thread-Safe Singletons: MongoDBSingleton facilitates the creation of MongoDB connections unique to each thread, preventing contention and ensuring thread safety.
  • Asyncio (ASGI) Support: MongoDBSingletonAsync extends the functionality for asynchronous applications, creating dedicated connections for each asyncio task.
  • Resource Optimization: mdb_singleton eliminates unnecessary connection overhead by reusing existing connections within the context of a thread or asyncio task.

Usage

Basic Usage

from mdb_singleton import MongoDBSingleton

# Create or retrieve a MongoDB connection singleton for the current thread
mongo_conn = MongoDBSingleton()
client = mongo_conn.client

# Your MongoDB operations here...

# Avoid closing the connection manually, as the package is designed for reuse.

ASGI (Asyncio) Usage

from mdb_singleton import MongoDBSingletonAsync

# Create or retrieve an async MongoDB connection singleton for the current asyncio task
mongo_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 Exit

To ensure proper closure of all MongoDB connections initiated with MongoDBSingleton and MongoDBSingletonAsync when the program or server terminates, include the following line of code in your startup script, settings.py, or any relevant location:

import atexit
from mdb_singleton import MongoDBSingleton

# Register connection closure on application exit
atexit.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:

from mdb_singleton import MongoDBSingleton

mongo_conn = MongoDBSingleton()
client = mongo_conn.client

# Your MongoDB operations here...

# Close the MongoDB connection manually
MongoDBSingleton.close_connection(mongo_conn.key)

This method will close the connection and remove the instance from the active connections.

Environment Variable

mdb_singleton expects the following environment variable to be set for MongoDB connection:

# .env file
MONGO_URI = "mongodb+srv://..."

Why Use mdb_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.

License

Copyright © 2024 nivwer.
This package is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mdb_singleton-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mdb_singleton-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file mdb_singleton-0.1.1.tar.gz.

File metadata

  • Download URL: mdb_singleton-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Linux/6.4.8-arch1-1

File hashes

Hashes for mdb_singleton-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1f40d52255b981047cd4d7fa9f6b71c877d4818b1a3b75b7665ea5da68168767
MD5 f5bf7997c2035cd043f7d55d07f0e1d6
BLAKE2b-256 f9f0ee31e6f113c53c4a98ce9cba101a3eba8d5fc8788ea3a7c63c246a46b356

See more details on using hashes here.

File details

Details for the file mdb_singleton-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mdb_singleton-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Linux/6.4.8-arch1-1

File hashes

Hashes for mdb_singleton-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ed02dab19c05c4729453335855a902a9dc6bd5a19a5d5deb003996fa28282fc
MD5 336f76e873d79374926cc6ae5b2c6fbc
BLAKE2b-256 bb24fa24e614c30c1cda6ba506733ec4080c59c1056eca23831c5ff2ca0fc1a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page