mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 09:38:21 +08:00
🐛 Fix: run_sync
上下文 (#1968)
This commit is contained in:
parent
50237fb778
commit
72b798f7ae
@ -12,6 +12,7 @@ import inspect
|
|||||||
import importlib
|
import importlib
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from contextvars import copy_context
|
||||||
from functools import wraps, partial
|
from functools import wraps, partial
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from typing_extensions import ParamSpec, get_args, get_origin
|
from typing_extensions import ParamSpec, get_args, get_origin
|
||||||
@ -111,7 +112,9 @@ def run_sync(call: Callable[P, R]) -> Callable[P, Coroutine[None, None, R]]:
|
|||||||
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
pfunc = partial(call, *args, **kwargs)
|
pfunc = partial(call, *args, **kwargs)
|
||||||
result = await loop.run_in_executor(None, pfunc)
|
context = copy_context()
|
||||||
|
context_run = context.run
|
||||||
|
result = await loop.run_in_executor(None, context_run, pfunc)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
return _wrapper
|
return _wrapper
|
||||||
|
Loading…
Reference in New Issue
Block a user