Title: Support for running code after the response has been returned · Issue #113 · aws/aws-lambda-python-runtime-interface-client · GitHub
Open Graph Title: Support for running code after the response has been returned · Issue #113 · aws/aws-lambda-python-runtime-interface-client
X Title: Support for running code after the response has been returned · Issue #113 · aws/aws-lambda-python-runtime-interface-client
Description: While this request might sound similar to the streaming response feature [see https://github.com//issues/100] now available to Node.js Lambda functions (see https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.ht...
Open Graph Description: While this request might sound similar to the streaming response feature [see https://github.com//issues/100] now available to Node.js Lambda functions (see https://docs.aws.amazon.com/lambda/lates...
X Description: While this request might sound similar to the streaming response feature [see https://github.com//issues/100] now available to Node.js Lambda functions (see https://docs.aws.amazon.com/lambda/lates...
Opengraph URL: https://github.com/aws/aws-lambda-python-runtime-interface-client/issues/113
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for running code after the response has been returned ","articleBody":"While this request might sound similar to the `streaming response` feature [see https://github.com/aws/aws-lambda-python-runtime-interface-client/issues/100] now available to Node.js Lambda functions (see https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html), it is a separate topic. \r\n\r\nWe have come across two different use cases where we'd like to run some Python code _after_ returning our response to, say, API Gateway. One use case is running Python's garbage collector \"manually\" instead of automatically. Rather than have Python decide to run garbage collection in the middle of our processing of an incoming `event`, and cause our users extra latency, we want to collect the garbage after returning the response. \r\n\r\nThe other use case involves updating DynamoDb with some bookkeeping about the `event` just serviced. While we can use an external Lambda Extension to do this, it requires moving a non-trivial amount of data to the Extension process, which seems wasteful assuming we could run this DynamoDb update after returning the response.\r\n\r\nAs a proof of concept, I wrote a monkeypatch to explore feasibility. I wrote it as a class that I can choose to instance or not at the top of my lambda handler.\r\n\r\n```python\r\nclass PostResponseHook:\r\n def __init__(self):\r\n import awslambdaric.bootstrap\r\n\r\n self.original_handle_event_request = awslambdaric.bootstrap.handle_event_request\r\n awslambdaric.bootstrap.handle_event_request = self.new_handle_event_request\r\n\r\n def new_handle_event_request(self, *args, **kwargs):\r\n self.original_handle_event_request(*args, **kwargs)\r\n self.hook()\r\n\r\n def hook(self, *args, **kwargs):\r\n # do something after the response was returned to the caller here\r\n pass\r\n```\r\n\r\nThe monkeypatch relies on the current structure of `handle_event_request` in [bootstrap.py](https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/main/awslambdaric/bootstrap.py). The Lambda function is not finished processing the incoming Event in `handle_event_request`, but, rather, continues back to the \"infinite loop\" in [run](https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/2d0c421ec588a05e6efd8773fa1416fe5433bae0/awslambdaric/bootstrap.py#L461C5-L461C8).\r\n\r\n```python\r\n while True:\r\n event_request = lambda_runtime_client.wait_next_invocation()\r\n\r\n _GLOBAL_AWS_REQUEST_ID = event_request.invoke_id\r\n\r\n update_xray_env_variable(event_request.x_amzn_trace_id)\r\n\r\n handle_event_request(\r\n lambda_runtime_client,\r\n request_handler,\r\n event_request.invoke_id,\r\n event_request.event_body,\r\n event_request.content_type,\r\n event_request.client_context,\r\n event_request.cognito_identity,\r\n event_request.invoked_function_arn,\r\n event_request.deadline_time_in_ms,\r\n log_sink,\r\n )\r\n```\r\n\r\nThe call to `lambda_runtime_client.wait_next_invocation()` in this loop is where the \"long poll\" for the next Lambda Event occurs (and, presumably, is where the Lambda service will freeze or shutdown this Lambda function instance under most circumstances (see https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html for details.)\r\n\r\n=============================================\r\n\r\nGiven that it seems technically feasible to offer this hook, how could we add this as a supported feature instead of relying on monkeypatching? It seems that we'd need a way to optionally \"register\" our hook method. Which would mean exposing a callable from somewhere in `awslambdaric`. Since I don't know of an existing API already exposed to user code by `awslambdaric`, I don't know where this `register_post_response_hook` method belongs. \r\n\r\nAlternatively, maybe opting in for this feature would be by means of some special Layer that `awslambdaric` would look for, avoiding the need for explicit opt-in registration by the user's handler? \r\n\r\n=============================================\r\n\r\nBut I'm probably getting ahead of myself here! The first question is: would the `awslambdaric` project consider this kind of hook as desirable, given that it can make a really big difference to latency-sensitive customer use-cases? ","author":{"url":"https://github.com/jrobbins-LiveData","@type":"Person","name":"jrobbins-LiveData"},"datePublished":"2023-09-07T11:49:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/113/aws-lambda-python-runtime-interface-client/issues/113"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:927f5f6e-1044-35cd-cc48-3f08906f55b2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8AB8:157A5D:899449:BAE78A:6A62E6B2 |
| html-safe-nonce | e875528e6f64e7ef17175faea60b73e676e1a9b9a04975784067ea483697faf6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUI4OjE1N0E1RDo4OTk0NDk6QkFFNzhBOjZBNjJFNkIyIiwidmlzaXRvcl9pZCI6IjI1MTIwMTM5MjIzODE1MjI2MTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8e5e9ef06bb565f892c7415bc553270d09a603e09639271a00185ce309f81523 |
| hovercard-subject-tag | issue:1885736171 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/aws/aws-lambda-python-runtime-interface-client/113/issue_layout |
| twitter:image | https://opengraph.githubassets.com/db36fcd6d091be74658fee4d431788c4a8c6ae22f2b92940095b1627afba2046/aws/aws-lambda-python-runtime-interface-client/issues/113 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/db36fcd6d091be74658fee4d431788c4a8c6ae22f2b92940095b1627afba2046/aws/aws-lambda-python-runtime-interface-client/issues/113 |
| og:image:alt | While this request might sound similar to the streaming response feature [see https://github.com//issues/100] now available to Node.js Lambda functions (see https://docs.aws.amazon.com/lambda/lates... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jrobbins-LiveData |
| hostname | github.com |
| expected-hostname | github.com |
| None | df33b1b61ee7b9a0af988199bfc3503c9c1acafb1f1d40e1f140ea7c84f890dd |
| turbo-cache-control | no-preview |
| go-import | github.com/aws/aws-lambda-python-runtime-interface-client git https://github.com/aws/aws-lambda-python-runtime-interface-client.git |
| octolytics-dimension-user_id | 2232217 |
| octolytics-dimension-user_login | aws |
| octolytics-dimension-repository_id | 292411453 |
| octolytics-dimension-repository_nwo | aws/aws-lambda-python-runtime-interface-client |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 292411453 |
| octolytics-dimension-repository_network_root_nwo | aws/aws-lambda-python-runtime-interface-client |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d41cd1bdb290013455c0ac430fa755621733f5eb |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width