perf(8502): 并行生图(6并发)+超时重试;视频URL直连预览/下载;路径隔离
This commit is contained in:
49
modules/limits.py
Normal file
49
modules/limits.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
Process-wide concurrency limits for Streamlit single-process deployment.
|
||||
|
||||
These limits reduce tail latency and avoid a single user saturating network/CPU
|
||||
and impacting other concurrent sessions.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import threading
|
||||
from contextlib import contextmanager
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
def _env_int(name: str, default: int) -> int:
|
||||
try:
|
||||
return max(1, int(os.getenv(name, str(default))))
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
MAX_CONCURRENT_IMAGE = _env_int("MAX_CONCURRENT_IMAGE", 6)
|
||||
MAX_CONCURRENT_VIDEO = _env_int("MAX_CONCURRENT_VIDEO", 1)
|
||||
|
||||
_image_sem = threading.BoundedSemaphore(MAX_CONCURRENT_IMAGE)
|
||||
_video_sem = threading.BoundedSemaphore(MAX_CONCURRENT_VIDEO)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def acquire_image(blocking: bool = True) -> Iterator[bool]:
|
||||
ok = _image_sem.acquire(blocking=blocking)
|
||||
try:
|
||||
yield ok
|
||||
finally:
|
||||
if ok:
|
||||
_image_sem.release()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def acquire_video(blocking: bool = True) -> Iterator[bool]:
|
||||
ok = _video_sem.acquire(blocking=blocking)
|
||||
try:
|
||||
yield ok
|
||||
finally:
|
||||
if ok:
|
||||
_video_sem.release()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user