perf: Remove is_async_callable check in request_response

This commit is contained in:
artemkucher 2026-02-07 19:25:04 +05:00
parent d06ab3f5c7
commit b8014715ca
1 changed files with 6 additions and 9 deletions

View File

@ -79,18 +79,15 @@ from starlette.websockets import WebSocket
from typing_extensions import deprecated from typing_extensions import deprecated
# Copy of starlette.routing.request_response modified to include the # Copy of starlette.routing.request_response, modified to include the
# dependencies' AsyncExitStack # dependencies' AsyncExitStack and to remove support for non-awaitable
# functions.
def request_response( def request_response(
func: Callable[[Request], Awaitable[Response] | Response], func: Callable[[Request], Awaitable[Response]],
) -> ASGIApp: ) -> ASGIApp:
""" """
Takes a function or coroutine `func(request) -> response`, Takes a coroutine `func(request) -> response` and returns an ASGI application.
and returns an ASGI application.
""" """
f: Callable[[Request], Awaitable[Response]] = (
func if is_async_callable(func) else functools.partial(run_in_threadpool, func) # type:ignore
)
async def app(scope: Scope, receive: Receive, send: Send) -> None: async def app(scope: Scope, receive: Receive, send: Send) -> None:
request = Request(scope, receive, send) request = Request(scope, receive, send)
@ -102,7 +99,7 @@ def request_response(
scope["fastapi_inner_astack"] = request_stack scope["fastapi_inner_astack"] = request_stack
async with AsyncExitStack() as function_stack: async with AsyncExitStack() as function_stack:
scope["fastapi_function_astack"] = function_stack scope["fastapi_function_astack"] = function_stack
response = await f(request) response = await func(request)
await response(scope, receive, send) await response(scope, receive, send)
# Continues customization # Continues customization
response_awaited = True response_awaited = True