-
Notifications
You must be signed in to change notification settings - Fork 418
Feature request: Async support for ApiGatewayResolver #3934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hey @pkucmus! Wowww, we're diving into some pretty cool Feature Request here: making our EventHandler and all its resolvers work async and add more flexibility to enhance I/O operations. But it comes with some significant changes ahead. To be completely transparent, I'm unable to quantify the extent of work needed to implement this capability right now. Perhaps we should consider drafting an RFC to outline the process in detail. It might even be worth exploring the creation of entirely new resolvers that operate entirely async. It's encouraging to see "thumbs up" from many people regarding this matter, and I believe we should seriously consider it. To ensure we stay on track, I'll add some labels such as "revisit in 3 months" and "need help" to keep this issue on our radar and facilitate brainstorming for a solution. Thank you and please let us know if you have in mind any kind of implementation or solution for this. |
Thank you @leandrodamascena, what a nice approach for a random idea like that. I would be happy to put in some work into this. Maybe this would not be as replacement for the existing resolvers but something additional? If you would like I could draft something but I would need to understand how it's currently working, i.e. the use of the global state is confusing me a lot - I'm sure there's a reason for it but I don't know why we're storing and accessing the event in a powertools-lambda-python/aws_lambda_powertools/event_handler/api_gateway.py Lines 1814 to 1815 in c3e36de
|
We're truly appreciate the possibility of you working on a draft for how we can incorporate support for Async in our resolvers and make it work.
This code is a little older, probably from when we created the Event Handler utility, but it is still in use. Payloads are different when working with Event Handler, and we store the event and context in the ALB - https://github.com/aws-powertools/powertools-lambda-python/blob/develop/tests/events/albEvent.json All of our resolvers inherit from Does this explanation cover what you need, or would you like more information? Feel free to ask if you need further clarification!
Of course, there is! Please feel free to share any questions or blocks you have, and we can collaborate to find the best way to move forward. Thank you for taking the time to collaborate on this matter! 🌟 |
I don't know if this is entirely correct but I'm solving it this way: from functools import wraps
import asyncio
from aws_lambda_powertools import Tracer
from aws_lambda_powertools.event_handler import APIGatewayResolver
tracer = Tracer()
app = ApiGatewayResolver()
def wrap_async(func):
@wraps(func)
def wrapper(*args, **kwargs):
return asyncio.run(func(*args,**kwargs))
return wrapper
@tracer.capture_method
@app.get("/hello")
@wrap_async
async def hello():
return { "message": "world" }
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context) |
Hey @sykire, I tried your decorator and it indeed works by leveraging asyncio.run to manage the event loop for each synchronous call to our endpoints. However, this approach seems to counteract the benefits of asynchronous programming due to the significant performance overhead introduced, which I'm yet to find out why (im using I also wanted to discuss why incorporating async programming is important, especially in the context of API Gateway. In many projects, I've implemented the API Composition pattern, where the API Gateway is responsible for querying multiple services and aggregating their responses. It's very common API Gateways are responsible for doing this. This model inherently benefits from asynchronous code, as it allows for concurrent calls to multiple services, enhancing efficiency and reducing total response time from the server. For instance, if my Lambda function needs to query 5 different AWS services, firing all these requests simultaneously (assuming independence among them) is far more efficient than making sequential calls. Given that Lambda functions already have latency issues by nature, adding synchronous operations only worsens the response time. Moreover, I encountered compatibility issues with the X-Ray SDK when using aioboto3 for making concurrent AWS service calls via By not using |
After adding some debug time logs and tests I can confirm that doing However, notice a 800ms gap in every request with my new async code, so I think it has to do with how the resources are being reused for subsequent calls and how I'm using the I found this thread: https://stackoverflow.com/questions/60455830/can-you-have-an-async-handler-in-lambda-python-3-6
And he also suggests adding the async boto clients at the top of the module (I was creating them in the function every time): # To reduce execution time for subsequent invocations,
# open a reusable resource in a global scope
dynamodb = aioboto3.Session().resource('dynamodb') I'm going to give this a try and share results. def run_in_asyncio_event_loop(func):
"""
This decorator is used to run a function in the asyncio event loop.
This allows the Lambda function to run async code and reuse the same event loop between
executions, as well as resources defined in the global scope such as boto3 clients.
"""
@wraps(func)
def async_to_sync_wrapper(*args, **kwargs):
loop = asyncio.get_event_loop()
return loop.run_until_complete(func(*args, **kwargs))
return async_to_sync_wrapper |
Ok, after my tests I can confirm that doing the above reduces the latency in subsequent calls effectively reusing the event loop + boto3 clients. My only and big issue right now, is that when I use I followed the docs here https://docs.powertools.aws.dev/lambda/python/2.41.0/core/tracer/#concurrent-asynchronous-functions but it's not working. I've tried almost every possible combination and reordering the decorators. My code current looks like this: @app.get("/<executionArn>")
@run_in_asyncio_event_loop
@tracer.capture_method
async def get_url(
executionArn: str,
) ->
...
asyncio.gather(....) # some coroutines here Then, as explained in the docs, each coroutine uses @heitorlessa tagging you in hopes you might be able to help us, since I saw you contributed to this part a lot |
Hey everyone! I'm still in the process of creating a PoC for this new feature. However, it's turning out to be a more significant undertaking than I thought. Here's why:
I'll continue to investigate and work on the PoC, but I wanted to set realistic expectations about the timeline and complexity involved. Given the scope of this project, we're looking at a completion timeframe towards the end of this year or potentially extending into 2026. I appreciate any help here. I'll keep you updated as I make progress or if I discover any alternative approaches. |
Use case
I would love to be able to use asynchronous Python with Powertools. I understand there is not as much need for it in a Lambda runtime as a Lambda (process) will handle only one event but I would still be able to use async-only libraries like encode/databases (with the likes of Neon) or simply reduce my execution time by utilizing
asyncio.gather
when doing concurrent waits for I/O operations.Solution/User Experience
I imagine being able to use it similarly to what the GraphQL resolver does:
Alternative solutions
No response
Acknowledgment
The text was updated successfully, but these errors were encountered: