mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 12:32:30 +08:00
Move some functions out of 'api/utils/common.py' (#10948)
### What problem does this PR solve? as title. ### Type of change - [x] Refactoring Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@ -14,12 +14,6 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import threading
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
|
||||
def string_to_bytes(string):
|
||||
return string if isinstance(
|
||||
string, bytes) else string.encode(encoding="utf-8")
|
||||
@ -28,70 +22,3 @@ def string_to_bytes(string):
|
||||
def bytes_to_string(byte):
|
||||
return byte.decode(encoding="utf-8")
|
||||
|
||||
|
||||
def convert_bytes(size_in_bytes: int) -> str:
|
||||
"""
|
||||
Format size in bytes.
|
||||
"""
|
||||
if size_in_bytes == 0:
|
||||
return "0 B"
|
||||
|
||||
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
|
||||
i = 0
|
||||
size = float(size_in_bytes)
|
||||
|
||||
while size >= 1024 and i < len(units) - 1:
|
||||
size /= 1024
|
||||
i += 1
|
||||
|
||||
if i == 0 or size >= 100:
|
||||
return f"{size:.0f} {units[i]}"
|
||||
elif size >= 10:
|
||||
return f"{size:.1f} {units[i]}"
|
||||
else:
|
||||
return f"{size:.2f} {units[i]}"
|
||||
|
||||
|
||||
def once(func):
|
||||
"""
|
||||
A thread-safe decorator that ensures the decorated function runs exactly once,
|
||||
caching and returning its result for all subsequent calls. This prevents
|
||||
race conditions in multi-threaded environments by using a lock to protect
|
||||
the execution state.
|
||||
|
||||
Args:
|
||||
func (callable): The function to be executed only once.
|
||||
|
||||
Returns:
|
||||
callable: A wrapper function that executes `func` on the first call
|
||||
and returns the cached result thereafter.
|
||||
|
||||
Example:
|
||||
@once
|
||||
def compute_expensive_value():
|
||||
print("Computing...")
|
||||
return 42
|
||||
|
||||
# First call: executes and prints
|
||||
# Subsequent calls: return 42 without executing
|
||||
"""
|
||||
executed = False
|
||||
result = None
|
||||
lock = threading.Lock()
|
||||
def wrapper(*args, **kwargs):
|
||||
nonlocal executed, result
|
||||
with lock:
|
||||
if not executed:
|
||||
executed = True
|
||||
result = func(*args, **kwargs)
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
@once
|
||||
def pip_install_torch():
|
||||
device = os.getenv("DEVICE", "cpu")
|
||||
if device=="cpu":
|
||||
return
|
||||
logging.info("Installing pytorch")
|
||||
pkg_names = ["torch>=2.5.0,<3.0.0"]
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", *pkg_names])
|
||||
|
||||
Reference in New Issue
Block a user