async_helpers.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Async helper functions for web UI
  14. """
  15. import asyncio
  16. import sys
  17. import tomllib
  18. from pathlib import Path
  19. from loguru import logger
  20. def run_async(coro):
  21. """Run async coroutine in sync context"""
  22. if sys.platform == "win32":
  23. # Streamlit/Tornado may switch the global asyncio policy to
  24. # WindowsSelectorEventLoopPolicy, which breaks subprocess-based
  25. # libraries such as Playwright on Windows. Use an explicit
  26. # Proactor loop here so this sync bridge does not depend on the
  27. # ambient global policy.
  28. loop = asyncio.ProactorEventLoop()
  29. try:
  30. return loop.run_until_complete(coro)
  31. finally:
  32. try:
  33. from pixelle_video.services.frame_html import HTMLFrameGenerator
  34. loop.run_until_complete(HTMLFrameGenerator.close_browser())
  35. except Exception as e:
  36. logger.debug(f"Failed to cleanup HTML frame browser before loop close: {e}")
  37. loop.close()
  38. return asyncio.run(coro)
  39. def get_project_version():
  40. """Get project version from pyproject.toml"""
  41. try:
  42. # Get project root (web parent directory)
  43. web_dir = Path(__file__).resolve().parent.parent
  44. project_root = web_dir.parent
  45. pyproject_path = project_root / "pyproject.toml"
  46. if pyproject_path.exists():
  47. with open(pyproject_path, "rb") as f:
  48. pyproject_data = tomllib.load(f)
  49. return pyproject_data.get("project", {}).get("version", "Unknown")
  50. except Exception as e:
  51. logger.warning(f"Failed to read version from pyproject.toml: {e}")
  52. return "Unknown"