Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't run sync dependencies in threads by default #109

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xpresso"
version = "0.42.4"
version = "0.43.0"
description = "A developer centric, performant Python web framework"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down
4 changes: 3 additions & 1 deletion xpresso/_utils/endpoint_dependant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing

from di.api.providers import CallableProvider, CoroutineProvider
from di.concurrency import as_async
from di.dependant import Dependant

from xpresso.dependencies._dependencies import Depends, DependsMarker
Expand All @@ -16,12 +17,13 @@ def __init__(
endpoint: Endpoint,
sync_to_thread: bool = False,
) -> None:
if sync_to_thread:
endpoint = as_async(endpoint)
super().__init__(
call=endpoint,
scope="endpoint",
use_cache=False,
wire=True,
sync_to_thread=sync_to_thread,
)

def get_default_marker(self) -> DependsMarker[None]:
Expand Down
1 change: 0 additions & 1 deletion xpresso/_utils/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def hook(
scope=scope, # type: ignore[arg-type]
use_cache=dependant.use_cache,
wire=dependant.wire,
sync_to_thread=dependant.sync_to_thread,
)
if param is not None and param.annotation is not param.empty:
type_ = get_type(param)
Expand Down
16 changes: 12 additions & 4 deletions xpresso/dependencies/_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,32 @@ def __init__(
*,
use_cache: bool = True,
wire: bool = True,
sync_to_thread: bool = True,
sync_to_thread: bool = False,
scope: typing.Optional[Scope] = None,
) -> None:
super().__init__(
call=call,
scope=scope,
use_cache=use_cache,
wire=wire,
sync_to_thread=sync_to_thread,
)
self.sync_to_thread = sync_to_thread

def as_dependant(self) -> Dependant[DependencyType]:
call: "typing.Optional[DependencyProvider]"
if self.sync_to_thread:
if not self.call:
raise ValueError(
"sync_to_thread can only be used if you explicitly declare the target function"
)
call = as_async(self.call)
else:
call = self.call
return Dependant(
call=self.call,
call=call, # type: ignore
scope=self.scope,
use_cache=self.use_cache,
wire=self.wire,
sync_to_thread=self.sync_to_thread,
marker=self,
)

Expand Down
2 changes: 1 addition & 1 deletion xpresso/routing/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(
typing.Callable[[typing.Any], Response]
] = None,
response_encoder: typing.Optional[Encoder] = JsonableEncoder(),
sync_to_thread: bool = True,
sync_to_thread: bool = False,
# responses
response_status_code: int = 200,
response_media_type: str = "application/json",
Expand Down