""" 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()