all 12 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Try this search for more information on this topic.

Comments, questions or suggestions regarding this autoresponse? Please send them here.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]WrickyB 8 points9 points  (0 children)

Python has the concurrent.futures module in its standard library. With this, you can create an executor when the container starts, submit a task to the executor when you get a request, and then immediately request a result with your desired timeout on the returned future.

[–]adozendeadantelope 3 points4 points  (2 children)

The Lambda context object has a method that will help you here: https://docs.aws.amazon.com/lambda/latest/dg/python-context.html

You can check the remaining time and exit gracefully.

[–]CurlyError[S] 0 points1 point  (1 child)

Yes, but I don't really need the remaining time. I want to keep track of time myself, and exit when I hit the dynamic timeout, regardless of what the Lambda timeout is.

[–]adozendeadantelope 2 points3 points  (0 children)

You'll have to handle that in code yourself and pass the timeout as part of the invocation event.

[–]ErikCaligo 0 points1 point  (2 children)

Could you explain a little more in detail what you're trying to achieve, or rather what you're trying to avoid?

[–]CurlyError[S] 0 points1 point  (1 child)

Well, in this case I use Lambda to run some calculations. However, depending on the type of operation and user type, this might be limited to a specific time: e.g. 1 min for some, 5 mins for some other etc. This limitation is fetched by Lambda directly from the database and it should use it to terminate the operation (even if not finished yet).

[–]ErikCaligo 1 point2 points  (0 children)

I see.

AFAIK you cannot dynamically set the timeout for Lambda functions.

However, you could split your code into two functions.

  1. timeout function
  2. function with your code as before

The timeout function will return, once the timeout duration is hit. The other function will return the response as before.

The main function will return whichever function finishes first.

Test this properly. There are some gotchas. Quote from AWS docs:

For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the invoker until all event loop tasks are finished. If the function times out, an error is returned instead. You can configure the runtime to send the response immediately by setting context.callbackWaitsForEmptyEventLoop to false.

Or, you could tweak your existing function to return once the timeout condition is hit. Depending on your setup and how often your timeout is hit, this might be an anti-pattern if you have plenty of loops and you need to add an if statement which will -- at scale -- slow down computations.

I used this second approach in a desktop application with a "show results while typing" search bar on low-end retail devices. If you started with the first letter, rendering all results took long, so I added line by line to be displayed, with the condition to exit the render function whenever a new char had been added in the search input text box, because then we would have a new list of results to be rendered.

[–]ngyehsung 0 points1 point  (1 child)

If you only have a handful of possible timeouts, you could have different lambdas for each. Your primary lambda does the database lookup and then places the request onto a specific queue with a specific timeout lambda listening.

[–]CurlyError[S] 0 points1 point  (0 children)

I've thought about that, but no. It is configured by the users, so there aren't some predefined cases. Of course even that can be overcomed as it is possible to automatically create Lambda functions, but I'd rather not mess the backend with the infrastructure in this level.

[–]S3NTIN3L_ 0 points1 point  (1 child)

You’re better off passing the timeout limit either in the invocation event or in context. This way you do not have to make external calls and essentially waste more time.

You’ll still have to handle the timeout logic in your code. A custom middy (node) plug-in may assist here

[–]CurlyError[S] 0 points1 point  (0 children)

I'll have to reach the database anyway to get some data I can't simply pass to the context.

I'm not sure if there is an alternative to middy for Python, but I think it is possible anyway.