]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:recycle: Refactor function calling a path operation function to simplify profiling...
authorsm-Fifteen <516999+sm-Fifteen@users.noreply.github.com>
Sat, 29 Feb 2020 16:28:30 +0000 (11:28 -0500)
committerGitHub <noreply@github.com>
Sat, 29 Feb 2020 16:28:30 +0000 (17:28 +0100)
fastapi/routing.py

index 7c626af410889d8cccda60e08e1bc6682aa9eb62..05919be662ca3ca65597deda348b72faeacc4d6a 100644 (file)
@@ -87,6 +87,19 @@ async def serialize_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(
     dependant: Dependant,
     body_field: ModelField = None,
@@ -128,11 +141,10 @@ def get_request_handler(
         if errors:
             raise RequestValidationError(errors, body=body)
         else:
-            assert dependant.call is not None, "dependant.call must be a function"
-            if is_coroutine:
-                raw_response = await dependant.call(**values)
-            else:
-                raw_response = await run_in_threadpool(dependant.call, **values)
+            raw_response = await run_endpoint_function(
+                dependant=dependant, values=values, is_coroutine=is_coroutine
+            )
+
             if isinstance(raw_response, Response):
                 if raw_response.background is None:
                     raw_response.background = background_tasks