mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Adding semaphore usage on the '/run' endpoint (#8526)
### What problem does this PR solve? Switching threading.Lock() to asyncio.Lock(), since threading.Lock() is blocking. ### Type of change - [x] Performance Improvement
This commit is contained in:
@ -17,7 +17,6 @@ import asyncio
|
||||
import contextlib
|
||||
import os
|
||||
from queue import Empty, Queue
|
||||
from threading import Lock
|
||||
|
||||
from models.enums import SupportLanguage
|
||||
from util import env_setting_enabled, is_valid_memory_limit
|
||||
@ -26,18 +25,22 @@ from utils.common import async_run_command
|
||||
from core.logger import logger
|
||||
|
||||
_CONTAINER_QUEUES: dict[SupportLanguage, Queue] = {}
|
||||
_CONTAINER_LOCK: Lock = Lock()
|
||||
_CONTAINER_LOCK: asyncio.Lock = asyncio.Lock()
|
||||
_CONTAINER_EXECUTION_SEMAPHORES:dict[SupportLanguage,asyncio.Semaphore] = {}
|
||||
|
||||
|
||||
async def init_containers(size: int) -> tuple[int, int]:
|
||||
global _CONTAINER_QUEUES
|
||||
_CONTAINER_QUEUES = {SupportLanguage.PYTHON: Queue(), SupportLanguage.NODEJS: Queue()}
|
||||
|
||||
with _CONTAINER_LOCK:
|
||||
async with _CONTAINER_LOCK:
|
||||
while not _CONTAINER_QUEUES[SupportLanguage.PYTHON].empty():
|
||||
_CONTAINER_QUEUES[SupportLanguage.PYTHON].get_nowait()
|
||||
while not _CONTAINER_QUEUES[SupportLanguage.NODEJS].empty():
|
||||
_CONTAINER_QUEUES[SupportLanguage.NODEJS].get_nowait()
|
||||
|
||||
for language in SupportLanguage:
|
||||
_CONTAINER_EXECUTION_SEMAPHORES[language] = asyncio.Semaphore(size)
|
||||
|
||||
create_tasks = []
|
||||
for i in range(size):
|
||||
@ -56,7 +59,7 @@ async def init_containers(size: int) -> tuple[int, int]:
|
||||
|
||||
|
||||
async def teardown_containers():
|
||||
with _CONTAINER_LOCK:
|
||||
async with _CONTAINER_LOCK:
|
||||
while not _CONTAINER_QUEUES[SupportLanguage.PYTHON].empty():
|
||||
name = _CONTAINER_QUEUES[SupportLanguage.PYTHON].get_nowait()
|
||||
await async_run_command("docker", "rm", "-f", name, timeout=5)
|
||||
@ -151,7 +154,7 @@ async def recreate_container(name: str, language: SupportLanguage) -> bool:
|
||||
|
||||
async def release_container(name: str, language: SupportLanguage):
|
||||
"""Asynchronously release a container"""
|
||||
with _CONTAINER_LOCK:
|
||||
async with _CONTAINER_LOCK:
|
||||
if await container_is_running(name):
|
||||
_CONTAINER_QUEUES[language].put(name)
|
||||
logger.info(f"🟢 Released container: {name} (remaining available: {_CONTAINER_QUEUES[language].qsize()})")
|
||||
@ -168,7 +171,7 @@ async def allocate_container_blocking(language: SupportLanguage, timeout=10) ->
|
||||
while asyncio.get_running_loop().time() - start_time < timeout:
|
||||
try:
|
||||
name = _CONTAINER_QUEUES[language].get_nowait()
|
||||
with _CONTAINER_LOCK:
|
||||
async with _CONTAINER_LOCK:
|
||||
if not await container_is_running(name) and not await recreate_container(name, language):
|
||||
continue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user