您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. from contextlib import asynccontextmanager
  18. from fastapi import FastAPI
  19. from util import format_timeout_duration, parse_timeout_duration
  20. from core.container import init_containers, teardown_containers
  21. from core.logger import logger
  22. TIMEOUT = 10
  23. @asynccontextmanager
  24. async def _lifespan(app: FastAPI):
  25. """Asynchronous lifecycle management"""
  26. size = int(os.getenv("SANDBOX_EXECUTOR_MANAGER_POOL_SIZE", 1))
  27. success_count, total_task_count = await init_containers(size)
  28. logger.info(f"\n📊 Container pool initialization complete: {success_count}/{total_task_count} available")
  29. yield
  30. await teardown_containers()
  31. def init():
  32. TIMEOUT = parse_timeout_duration(os.getenv("SANDBOX_TIMEOUT"))
  33. logger.info(f"Global timeout: {format_timeout_duration(TIMEOUT)}")
  34. return _lifespan