mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
refactor: 移除 Python 后端源码,全面迁移至 Rust gateway 架构
- 删除全部 Python 源码 (src/) 及 Alembic 迁移脚本,归档至 _deprecated_py_src/ - 重构 Rust gateway ai_pipeline: 拆分 planner/finalize 模块,新增 contracts/adaptation 层 - 重组 handlers 模块为 admin/public/proxy/internal/shared 子模块结构 - 新增 executor 模块,引入 Rust 原生数据库迁移 (aether-data/migrations) - 简化 CI/Docker 构建流程,移除 base image 二级构建,统一为单一 app image - 移除 Python 相关基础设施文件 (entrypoint.sh, gunicorn_conf.py, Dockerfile.base)
This commit is contained in:
66
_deprecated_py_src/utils/async_utils.py
Normal file
66
_deprecated_py_src/utils/async_utils.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""
|
||||
异步工具函数
|
||||
|
||||
提供在异步上下文中安全执行同步函数的工具,避免阻塞事件循环。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable, Coroutine
|
||||
from functools import partial, wraps
|
||||
from typing import Any, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
# 全局 task 引用集合,防止 fire-and-forget task 被 GC 回收
|
||||
_background_tasks: set[asyncio.Task[Any]] = set()
|
||||
|
||||
|
||||
def safe_create_task(coro: Coroutine[Any, Any, Any]) -> asyncio.Task[Any] | None:
|
||||
"""创建后台 task 并持有引用,防止被 GC 回收。
|
||||
|
||||
用于 fire-and-forget 场景(如缓存失效、异步指标上报等),
|
||||
替代裸 ``asyncio.create_task()`` 调用。
|
||||
|
||||
Returns:
|
||||
创建的 Task 对象;若没有运行中的事件循环则返回 None。
|
||||
"""
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
return None
|
||||
task = loop.create_task(coro)
|
||||
_background_tasks.add(task)
|
||||
task.add_done_callback(_background_tasks.discard)
|
||||
return task
|
||||
|
||||
|
||||
async def run_in_executor(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
|
||||
"""
|
||||
在线程池中运行同步函数,避免阻塞事件循环。
|
||||
|
||||
用法:
|
||||
result = await run_in_executor(some_sync_function, arg1, arg2)
|
||||
"""
|
||||
loop = asyncio.get_running_loop()
|
||||
bound = partial(func, *args, **kwargs)
|
||||
return await loop.run_in_executor(None, bound)
|
||||
|
||||
|
||||
def async_wrap_sync(func: Callable[..., T]) -> Callable[..., Coroutine[Any, Any, T]]:
|
||||
"""
|
||||
装饰器:将同步函数包装成异步函数(在线程池中执行)。
|
||||
|
||||
用法:
|
||||
@async_wrap_sync
|
||||
def do_sync(...): ...
|
||||
|
||||
result = await do_sync(...)
|
||||
"""
|
||||
|
||||
@wraps(func)
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> T:
|
||||
return await run_in_executor(func, *args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user