Hey all,
Not sure if this should go here or if it should go into another Python sub. I was thinking of posting this in r/fastapi but it’s not exactly specific to it. I’m relatively new to Python’s approach to async/await and am still wrapping my head around it. I’ve had some experience with in Javascript, but as I understand it the approach is quite different in Python.
My team was prototyping a new feature and used FastAPI to build out the endpoints. These endpoints are implemented asynchronously. Now that we’ve released the feature in beta, we’re going back to see where we can implement some performance improvements. Essentially, it’s a lot of…
route.post(...) async def foo(...): # ...do some compute some_data = call_external_service_a() # ...do some more compute more_data = call_external_service_b(some_data)
I initially thought that if we make these external service calls async, we could get some performance improvements. However, someone on my team said since the endpoint itself is already async, there’s no point in essentially “nesting” more async functions underneath because FastAPI is already awaiting the execution of foo
here. She specifically shared this from FastAPI’s routing file:
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: ...
Naively this kind of makes sense, since you’re awaiting the entire endpoint call already, why complicate your code with more? But it also sounds wrong, because the implication is that you don’t need async/await anywhere except at the top level function.
Thanks in advance for any help here! I would also welcome any good resources you all have that’ll help me better understand how asynchronous programming works in Python.
submitted by /u/onefutui2e
[link] [comments]
r/learnpython Hey all, Not sure if this should go here or if it should go into another Python sub. I was thinking of posting this in r/fastapi but it’s not exactly specific to it. I’m relatively new to Python’s approach to async/await and am still wrapping my head around it. I’ve had some experience with in Javascript, but as I understand it the approach is quite different in Python. My team was prototyping a new feature and used FastAPI to build out the endpoints. These endpoints are implemented asynchronously. Now that we’ve released the feature in beta, we’re going back to see where we can implement some performance improvements. Essentially, it’s a lot of… route.post(…) async def foo(…): # …do some compute some_data = call_external_service_a() # …do some more compute more_data = call_external_service_b(some_data) I initially thought that if we make these external service calls async, we could get some performance improvements. However, someone on my team said since the endpoint itself is already async, there’s no point in essentially “nesting” more async functions underneath because FastAPI is already awaiting the execution of foo here. She specifically shared this from FastAPI’s routing file: 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: … Naively this kind of makes sense, since you’re awaiting the entire endpoint call already, why complicate your code with more? But it also sounds wrong, because the implication is that you don’t need async/await anywhere except at the top level function. Thanks in advance for any help here! I would also welcome any good resources you all have that’ll help me better understand how asynchronous programming works in Python. submitted by /u/onefutui2e [link] [comments]
Hey all,
Not sure if this should go here or if it should go into another Python sub. I was thinking of posting this in r/fastapi but it’s not exactly specific to it. I’m relatively new to Python’s approach to async/await and am still wrapping my head around it. I’ve had some experience with in Javascript, but as I understand it the approach is quite different in Python.
My team was prototyping a new feature and used FastAPI to build out the endpoints. These endpoints are implemented asynchronously. Now that we’ve released the feature in beta, we’re going back to see where we can implement some performance improvements. Essentially, it’s a lot of…
route.post(...) async def foo(...): # ...do some compute some_data = call_external_service_a() # ...do some more compute more_data = call_external_service_b(some_data)
I initially thought that if we make these external service calls async, we could get some performance improvements. However, someone on my team said since the endpoint itself is already async, there’s no point in essentially “nesting” more async functions underneath because FastAPI is already awaiting the execution of foo
here. She specifically shared this from FastAPI’s routing file:
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: ...
Naively this kind of makes sense, since you’re awaiting the entire endpoint call already, why complicate your code with more? But it also sounds wrong, because the implication is that you don’t need async/await anywhere except at the top level function.
Thanks in advance for any help here! I would also welcome any good resources you all have that’ll help me better understand how asynchronous programming works in Python.
submitted by /u/onefutui2e
[link] [comments]