Skip to content

Commit 430acd6

Browse files
committed
feat: support Anyio totally
1 parent 1272208 commit 430acd6

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/fastapi_proxy_lib/core/_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ def return_err_msg_response(
309309
err_response_json = ErrRseponseJson(detail=detail)
310310

311311
# TODO: 请注意,logging是同步函数,每次会阻塞1ms左右,这可能会导致性能问题
312-
# 特别是对于写入文件的log,最好把它放到 asyncio.to_thread 里执行
313-
# https://docs.python.org/zh-cn/3/library/asyncio-task.html#coroutine
312+
# 特别是对于写入文件的log,最好把它放到 `anyio.to_thread.run_sync()` 里执行
313+
# https://anyio.readthedocs.io/en/stable/threads.html#running-a-function-in-a-worker-thread
314314

315315
if logger is not None:
316316
# 只要传入了logger,就一定记录日志

src/fastapi_proxy_lib/fastapi/router.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
The low-level API for [fastapi_proxy_lib.fastapi.app][].
44
"""
55

6-
import asyncio
76
import warnings
87
from contextlib import asynccontextmanager
98
from typing import (
@@ -18,6 +17,7 @@
1817
Union,
1918
)
2019

20+
import anyio
2121
from fastapi import APIRouter
2222
from starlette.requests import Request
2323
from starlette.responses import Response
@@ -273,6 +273,8 @@ async def shutdown_clients(*_: Any, **__: Any) -> AsyncIterator[None]:
273273
When __aexit__ is called, will close all registered proxy.
274274
"""
275275
yield
276-
await asyncio.gather(*[proxy.aclose() for proxy in self.registered_proxy])
276+
async with anyio.create_task_group() as tg:
277+
for proxy in self.registered_proxy:
278+
tg.start_soon(proxy.aclose)
277279

278280
return shutdown_clients

0 commit comments

Comments
 (0)