|
23 | 23 | from . import migrate |
24 | 24 | from .param import ORMParam |
25 | 25 | from .config import Config, plugin_config |
26 | | -from .utils import LoguruHandler, StreamToLogger, get_subclasses |
| 26 | +from .utils import LoguruHandler, StreamToLogger, coroutine, get_subclasses |
27 | 27 |
|
28 | 28 | if sys.version_info >= (3, 9): |
29 | 29 | from typing import Annotated |
@@ -118,18 +118,21 @@ def get_session(**local_kw: Any) -> sa_async.AsyncSession: |
118 | 118 | raise RuntimeError("nonebot-plugin-orm 未初始化") from None |
119 | 119 |
|
120 | 120 |
|
121 | | -AsyncSession = Annotated[sa_async.AsyncSession, Depends(get_session)] |
| 121 | +# NOTE: NoneBot DI will run sync function in thread pool executor, |
| 122 | +# which is poor performance for this simple function, so we wrap it as a coroutine function. |
| 123 | +AsyncSession = Annotated[sa_async.AsyncSession, Depends(coroutine(get_session))] |
122 | 124 |
|
123 | 125 |
|
124 | | -async def get_scoped_session() -> sa_async.async_scoped_session[sa_async.AsyncSession]: |
| 126 | +def get_scoped_session() -> sa_async.async_scoped_session[sa_async.AsyncSession]: |
125 | 127 | try: |
126 | 128 | return _scoped_sessions |
127 | 129 | except NameError: |
128 | 130 | raise RuntimeError("nonebot-plugin-orm 未初始化") from None |
129 | 131 |
|
130 | 132 |
|
131 | 133 | async_scoped_session = Annotated[ |
132 | | - sa_async.async_scoped_session[sa_async.AsyncSession], Depends(get_scoped_session) |
| 134 | + sa_async.async_scoped_session[sa_async.AsyncSession], |
| 135 | + Depends(coroutine(get_scoped_session)), |
133 | 136 | ] |
134 | 137 |
|
135 | 138 |
|
|
0 commit comments