Don't block the event loop on non-async functions (#1909)
This commit is contained in:
parent
5fdd48a0d3
commit
1c258ae203
1 changed files with 5 additions and 4 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
from collections.abc import Awaitable, Callable, Sequence
|
from collections.abc import Awaitable, Callable, Sequence
|
||||||
|
|
@ -5,6 +6,8 @@ from itertools import chain
|
||||||
from types import GenericAlias
|
from types import GenericAlias
|
||||||
from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints
|
from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints
|
||||||
|
|
||||||
|
import anyio
|
||||||
|
import anyio.to_thread
|
||||||
import pydantic_core
|
import pydantic_core
|
||||||
from pydantic import BaseModel, ConfigDict, Field, WithJsonSchema, create_model
|
from pydantic import BaseModel, ConfigDict, Field, WithJsonSchema, create_model
|
||||||
from pydantic.fields import FieldInfo
|
from pydantic.fields import FieldInfo
|
||||||
|
|
@ -53,9 +56,7 @@ class ArgModelBase(BaseModel):
|
||||||
kwargs[output_name] = value
|
kwargs[output_name] = value
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||||
arbitrary_types_allowed=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class FuncMetadata(BaseModel):
|
class FuncMetadata(BaseModel):
|
||||||
|
|
@ -85,7 +86,7 @@ class FuncMetadata(BaseModel):
|
||||||
if fn_is_async:
|
if fn_is_async:
|
||||||
return await fn(**arguments_parsed_dict)
|
return await fn(**arguments_parsed_dict)
|
||||||
else:
|
else:
|
||||||
return fn(**arguments_parsed_dict)
|
return await anyio.to_thread.run_sync(functools.partial(fn, **arguments_parsed_dict))
|
||||||
|
|
||||||
def convert_result(self, result: Any) -> Any:
|
def convert_result(self, result: Any) -> Any:
|
||||||
"""Convert the result of a function call to the appropriate format for
|
"""Convert the result of a function call to the appropriate format for
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue