mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-12 13:36:41 +00:00
♻️ Refactor function calling a path operation function to simplify profiling (#1027)
This commit is contained in:
parent
8e0200607f
commit
1ce67887b9
1 changed files with 17 additions and 5 deletions
|
|
@ -87,6 +87,19 @@ async def serialize_response(
|
||||||
return jsonable_encoder(response)
|
return jsonable_encoder(response)
|
||||||
|
|
||||||
|
|
||||||
|
async def run_endpoint_function(
|
||||||
|
*, dependant: Dependant, values: Dict[str, Any], is_coroutine: bool
|
||||||
|
) -> Any:
|
||||||
|
# Only called by get_request_handler. Has been split into its own function to
|
||||||
|
# facilitate profiling endpoints, since inner functions are harder to profile.
|
||||||
|
assert dependant.call is not None, "dependant.call must be a function"
|
||||||
|
|
||||||
|
if is_coroutine:
|
||||||
|
return await dependant.call(**values)
|
||||||
|
else:
|
||||||
|
return await run_in_threadpool(dependant.call, **values)
|
||||||
|
|
||||||
|
|
||||||
def get_request_handler(
|
def get_request_handler(
|
||||||
dependant: Dependant,
|
dependant: Dependant,
|
||||||
body_field: ModelField = None,
|
body_field: ModelField = None,
|
||||||
|
|
@ -128,11 +141,10 @@ def get_request_handler(
|
||||||
if errors:
|
if errors:
|
||||||
raise RequestValidationError(errors, body=body)
|
raise RequestValidationError(errors, body=body)
|
||||||
else:
|
else:
|
||||||
assert dependant.call is not None, "dependant.call must be a function"
|
raw_response = await run_endpoint_function(
|
||||||
if is_coroutine:
|
dependant=dependant, values=values, is_coroutine=is_coroutine
|
||||||
raw_response = await dependant.call(**values)
|
)
|
||||||
else:
|
|
||||||
raw_response = await run_in_threadpool(dependant.call, **values)
|
|
||||||
if isinstance(raw_response, Response):
|
if isinstance(raw_response, Response):
|
||||||
if raw_response.background is None:
|
if raw_response.background is None:
|
||||||
raw_response.background = background_tasks
|
raw_response.background = background_tasks
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue