Skip to content

Commit 1ce6788

Browse files
authored
♻️ Refactor function calling a path operation function to simplify profiling (fastapi#1027)
1 parent 8e02006 commit 1ce6788

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

fastapi/routing.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ async def serialize_response(
8787
return jsonable_encoder(response)
8888

8989

90+
async def run_endpoint_function(
91+
*, dependant: Dependant, values: Dict[str, Any], is_coroutine: bool
92+
) -> Any:
93+
# Only called by get_request_handler. Has been split into its own function to
94+
# facilitate profiling endpoints, since inner functions are harder to profile.
95+
assert dependant.call is not None, "dependant.call must be a function"
96+
97+
if is_coroutine:
98+
return await dependant.call(**values)
99+
else:
100+
return await run_in_threadpool(dependant.call, **values)
101+
102+
90103
def get_request_handler(
91104
dependant: Dependant,
92105
body_field: ModelField = None,
@@ -128,11 +141,10 @@ async def app(request: Request) -> Response:
128141
if errors:
129142
raise RequestValidationError(errors, body=body)
130143
else:
131-
assert dependant.call is not None, "dependant.call must be a function"
132-
if is_coroutine:
133-
raw_response = await dependant.call(**values)
134-
else:
135-
raw_response = await run_in_threadpool(dependant.call, **values)
144+
raw_response = await run_endpoint_function(
145+
dependant=dependant, values=values, is_coroutine=is_coroutine
146+
)
147+
136148
if isinstance(raw_response, Response):
137149
if raw_response.background is None:
138150
raw_response.background = background_tasks

0 commit comments

Comments
 (0)