返回 Pixelle-Video
async_helpers.py
根目录 / web / utils / async_helpers.py
1 # Copyright (C) 2025 AIDC-AI
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
12
13 """
14 Async helper functions for web UI
15 """
16
17 import asyncio
18 import sys
19 import tomllib
20 from pathlib import Path
21
22 from loguru import logger
23
24
25 def run_async(coro):
26 """Run async coroutine in sync context"""
27 if sys.platform == "win32":
28 # Streamlit/Tornado may switch the global asyncio policy to
29 # WindowsSelectorEventLoopPolicy, which breaks subprocess-based
30 # libraries such as Playwright on Windows. Use an explicit
31 # Proactor loop here so this sync bridge does not depend on the
32 # ambient global policy.
33 loop = asyncio.ProactorEventLoop()
34 try:
35 return loop.run_until_complete(coro)
36 finally:
37 try:
38 from pixelle_video.services.frame_html import HTMLFrameGenerator
39
40 loop.run_until_complete(HTMLFrameGenerator.close_browser())
41 except Exception as e:
42 logger.debug(f"Failed to cleanup HTML frame browser before loop close: {e}")
43 loop.close()
44 return asyncio.run(coro)
45
46
47 def get_project_version():
48 """Get project version from pyproject.toml"""
49 try:
50 # Get project root (web parent directory)
51 web_dir = Path(__file__).resolve().parent.parent
52 project_root = web_dir.parent
53 pyproject_path = project_root / "pyproject.toml"
54
55 if pyproject_path.exists():
56 with open(pyproject_path, "rb") as f:
57 pyproject_data = tomllib.load(f)
58 return pyproject_data.get("project", {}).get("version", "Unknown")
59 except Exception as e:
60 logger.warning(f"Failed to read version from pyproject.toml: {e}")
61 return "Unknown"
62
63
63 lines PYTHON