Question about custom event handling #256
-
The Mangum doc shows this example of how one might handle a custom event: def handler(event, context):
if event.get("some-key"):
# Do something or return, etc.
return
asgi_handler = Mangum(app)
response = asgi_handler(event, context) # Call the instance with the event arguments
return response I need to handle an incoming AWS EventBridge event. I want to invoke the same method that my HTTP API handler is also invoking -- and it is an async method. Here's what I am using def handler(event: LambdaEvent, context: LambdaContext) -> dict:
if "requestContext" in event:
logger.debug("HTTP API")
response = fastapi_handler(event, context)
else:
logger.debug("Not HTTP API")
loop = asyncio.get_event_loop()
response = loop.run_until_complete(app_logic())
return response My question(s): Is this the correct pattern to use? In the Mangum source, I see code like this, so I am trying to fit in. My code seems to work, but I read in the Python docs that
Which made me wonder if I were doing this correctly? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
I'm not sure exactly what your example code is attempting to do. Does the In any case, the |
Beta Was this translation helpful? Give feedback.
-
Hi @jordaneremieff . Sorry for the lack of clarity.
If I replace |
Beta Was this translation helpful? Give feedback.
-
@jordaneremieff this is still an issue for us. If a lambda handler function wants to either call a If I call |
Beta Was this translation helpful? Give feedback.
-
Coincidentally I've actually run into this issue myself because I was trying to develop a local dev server for testing the adapter using I'm going to convert this discussion into an issue now that I understand the problem correctly, but at this point I'm not certain the best way to resolve it - open to reviewing a PR to address this, otherwise I'll revisit when I have time. |
Beta Was this translation helpful? Give feedback.
-
Hi @jordaneremieff, It appears that the trend for
I think the solution might be to have the Mangum class explicitly create a new event loop in its This would allow Mangum's internals to switch away from the to-be-deprecated app = FastAPI()
fastapi_handler = Mangum(app)
def handler(event, context):
if 'requestContext' in event:
logger.debug('HTTP API')
response = fastapi_handler(event, context)
else:
logger.debug('Not HTTP API')
# somehow get the loop from Mangum; via the `asgi` `scope`?
response = loop.run_until_complete(sync_lastpass_to_cognito()) |
Beta Was this translation helpful? Give feedback.
-
I just tested in a Lambda (without editing Mangum) by putting this code up top in my Lambda file, and my use-case works. loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
app = FastAPI()
fastapi_handler = Mangum(app) Ideally we could put that loop creation code inside Mangum (easily done) and provide a way for the user to access that loop from Mangum. I don't know the right way to do that ... ought it simply be a |
Beta Was this translation helpful? Give feedback.
Hi @jrobbins-LiveData,
Coincidentally I've actually run into this issue myself because I was trying to develop a local dev server for testing the adapter using
asyncio.Protocol
, but I ended up hitting theRuntimeError: no running event loop
problem as well. I eventually shelved the idea for the time being, but I did come across https://github.com/erdewit/nest_asyncio which has been mentioned as a way to workaround this limitation.I'm going to convert this discussion into an issue now that I understand the problem correctly, but at this point I'm not certain the best way to resolve it - open to reviewing a PR to address this, otherwise I'll revisit when I have time.