A lightweight version of Milvus wrapped with Python for vector similarity search in AI applications
—
Thread-safe management of multiple milvus-lite server instances with automatic lifecycle handling and resource cleanup. The ServerManager provides a high-level interface for managing multiple independent databases in multi-tenant or multi-database scenarios.
Centralized manager for multiple server instances with thread-safe operations and automatic resource management.
class ServerManager:
"""
Thread-safe manager for multiple milvus-lite server instances.
Manages server lifecycle, prevents resource conflicts, and provides
automatic cleanup for all managed servers.
"""
def __init__(self):
"""Initialize server manager with thread lock and server registry."""
def start_and_get_uri(self, path: str, args=None) -> Optional[str]:
"""
Start server for given database path and return connection URI.
Creates or reuses existing server instance for the specified path.
Thread-safe operation with automatic server lifecycle management.
Parameters:
- path (str): Path to database file
- args: Additional server arguments (currently unused)
Returns:
- Optional[str]: Connection URI on success, None on failure
"""
def release_server(self, path: str) -> None:
"""
Stop and remove server for given database path.
Thread-safe operation that stops the server process, cleans up
resources, and removes it from the managed server registry.
Parameters:
- path (str): Path to database file
"""
def release_all(self) -> None:
"""
Stop and remove all managed servers.
Thread-safe bulk operation for cleanup of all server instances.
Useful for application shutdown or resource cleanup.
"""
def __del__(self) -> None:
"""Destructor that releases all servers when manager is destroyed."""Usage Example:
from milvus_lite.server_manager import server_manager_instance
# Start server and get connection URI
uri = server_manager_instance.start_and_get_uri("./database1.db")
if uri:
print(f"Database1 available at: {uri}")
# Use URI with pymilvus client
from pymilvus import MilvusClient
client = MilvusClient(uri=uri)
client.create_collection("test", dimension=128)
# Start additional servers for different databases
uri2 = server_manager_instance.start_and_get_uri("./database2.db")
uri3 = server_manager_instance.start_and_get_uri("/tmp/temp_vectors.db")
# Release specific server when done
server_manager_instance.release_server("./database1.db")
# Cleanup all servers (usually at application shutdown)
server_manager_instance.release_all()Pre-configured singleton instance for convenient server management across the application.
server_manager_instance: ServerManagerThis global instance is automatically created when importing the module and provides a convenient way to manage servers without manual instantiation.
Usage Example:
from milvus_lite.server_manager import server_manager_instance
# Use global instance directly
uri = server_manager_instance.start_and_get_uri("./app_database.db")
# The instance is shared across all imports
# Multiple modules can use the same server_manager_instanceManage multiple independent databases simultaneously with isolated server instances.
Usage Example:
from milvus_lite.server_manager import server_manager_instance
from pymilvus import MilvusClient
class MultiDatabaseApp:
def __init__(self):
self.databases = {}
def add_database(self, name: str, db_path: str):
"""Add a new database to the application."""
uri = server_manager_instance.start_and_get_uri(db_path)
if uri:
client = MilvusClient(uri=uri)
self.databases[name] = {
'uri': uri,
'path': db_path,
'client': client
}
print(f"Database '{name}' added successfully")
return True
return False
def remove_database(self, name: str):
"""Remove database from application."""
if name in self.databases:
db_path = self.databases[name]['path']
server_manager_instance.release_server(db_path)
del self.databases[name]
print(f"Database '{name}' removed")
def list_databases(self):
"""List all active databases."""
return list(self.databases.keys())
def get_client(self, name: str) -> Optional[MilvusClient]:
"""Get client for specific database."""
return self.databases.get(name, {}).get('client')
def shutdown(self):
"""Cleanup all databases."""
server_manager_instance.release_all()
self.databases.clear()
# Usage
app = MultiDatabaseApp()
app.add_database("users", "./users.db")
app.add_database("products", "./products.db")
app.add_database("analytics", "./analytics.db")
# Use different databases
user_client = app.get_client("users")
if user_client:
user_client.create_collection("profiles", dimension=256)
product_client = app.get_client("products")
if product_client:
product_client.create_collection("embeddings", dimension=512)
# Cleanup when done
app.shutdown()All ServerManager operations are thread-safe and can be used safely in multi-threaded applications.
Usage Example:
import threading
from milvus_lite.server_manager import server_manager_instance
def worker_thread(thread_id: int):
"""Worker function for multi-threaded database access."""
db_path = f"./worker_{thread_id}.db"
# Thread-safe server startup
uri = server_manager_instance.start_and_get_uri(db_path)
if uri:
# Each thread gets its own database instance
from pymilvus import MilvusClient
client = MilvusClient(uri=uri)
# Perform thread-specific operations
collection_name = f"thread_{thread_id}_data"
client.create_collection(collection_name, dimension=128)
# Insert some data specific to this thread
data = [{"id": i, "vector": [0.1 * thread_id] * 128}
for i in range(10)]
client.insert(collection_name, data)
print(f"Thread {thread_id} completed operations")
# Server cleanup is handled automatically by manager
# Start multiple worker threads
threads = []
for i in range(5):
thread = threading.Thread(target=worker_thread, args=(i,))
threads.append(thread)
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
# Cleanup all servers created by threads
server_manager_instance.release_all()The ServerManager automatically handles resource management including process cleanup, file locks, and memory management.
Automatic Cleanup:
Resource Limits:
Best Practices:
release_server() when done with specific databasesrelease_all() during application shutdownPath Handling:
Install with Tessl CLI
npx tessl i tessl/pypi-milvus-lite