Implementation:Infiniflow Ragflow Misc Utils
| Knowledge Sources | |
|---|---|
| Domains | Utilities, Infrastructure |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
Concrete collection of general-purpose utility functions including UUID generation, image downloading, byte formatting, run-once decorator, and thread pool execution provided by the RAGFlow common library.
Description
The misc_utils module provides: get_uuid for UUID v1 generation, download_img for fetching images as base64, hash_str2int for SHA1-based string hashing, convert_bytes for human-readable byte formatting, once as a thread-safe run-once decorator, and thread_pool_exec for asynchronous function execution in a shared thread pool.
Usage
Import specific utilities as needed across the codebase for UUID generation in database records, image downloading in document processing, or thread pool execution for I/O-bound operations.
Code Reference
Source Location
- Repository: Infiniflow_Ragflow
- File: common/misc_utils.py
- Lines: 1-134
Signature
def get_uuid() -> str:
"""Generate UUID v1 as hex string."""
def download_img(url: str) -> str:
"""Download image and return as base64 data URI."""
def hash_str2int(line: str, mod: int = 10**8) -> int:
"""Hash string to integer using SHA1."""
def convert_bytes(size_in_bytes: int) -> str:
"""Format bytes to human-readable size string."""
def once(func):
"""Thread-safe decorator ensuring function runs exactly once."""
def thread_pool_exec(func, *args, **kwargs) -> Future:
"""Execute function asynchronously in shared thread pool."""
Import
from common.misc_utils import get_uuid, convert_bytes, once, thread_pool_exec
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | str | Yes | Image URL (for download_img) |
| line | str | Yes | String to hash (for hash_str2int) |
| size_in_bytes | int | Yes | Byte count (for convert_bytes) |
| func | callable | Yes | Function to execute (for thread_pool_exec) |
Outputs
| Name | Type | Description |
|---|---|---|
| get_uuid() returns | str | 32-character hex UUID string |
| download_img() returns | str | Base64-encoded data URI |
| convert_bytes() returns | str | Formatted size (e.g., "1.5 GB") |
| thread_pool_exec() returns | Future | Future object for async result |
Usage Examples
from common.misc_utils import get_uuid, convert_bytes, thread_pool_exec
doc_id = get_uuid() # "550e8400e29b41d4a716446655440000"
size_str = convert_bytes(1536000) # "1.46 MB"
future = thread_pool_exec(expensive_computation, data)
result = future.result() # Block until complete