Implementation:Infiniflow Ragflow Singleton Decorator
| Knowledge Sources | |
|---|---|
| Domains | Design_Patterns, Infrastructure |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
Concrete tool providing a process-aware singleton decorator that ensures only one instance per class per process provided by the RAGFlow common library.
Description
The singleton decorator function creates a per-process singleton pattern using the process ID as a cache key. This ensures that each forked worker process gets its own singleton instance while preventing duplicate instantiation within the same process.
Usage
Apply this decorator to connection or service classes that should be instantiated only once per process, such as database connection pools or cache clients.
Code Reference
Source Location
- Repository: Infiniflow_Ragflow
- File: common/decorator.py
- Lines: 1-27
Signature
def singleton(cls, *args, **kw):
"""Process-aware singleton decorator. One instance per class per process."""
Import
from common.decorator import singleton
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cls | type | Yes | The class to make singleton |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | function | A wrapper that returns the cached instance |
Usage Examples
from common.decorator import singleton
@singleton
class DatabaseConnection:
def __init__(self):
self.conn = create_connection()
# Both calls return the same instance within a process
db1 = DatabaseConnection()
db2 = DatabaseConnection()
assert db1 is db2