Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when I start a new thread in lambda function and return

When I start a new thread in lambda function and return, it look like it stops execution of new threads and close.

public class Importer implements RequestHandler<Request<ImportJob>, Response> {

@Override
public Response handleRequest(final Request<ImportJob> request, final Context context) {

    CompletableFuture.runAsync(() -> uploadOriginalFile(importJob), ioThreadPool)
        .thenRunAsync(() -> convert(importJob), importThreadPool)
        .thenRun(() -> createThumbnail(importJob))
        .handleAsync((r, cause) -> completeJob(importJob, cause), ioThreadPool);

    return new Response("Execution started");
  }
}

When I wait for the execution of all CompletableFuture using join(), It run as I expect but when I try run it in aysnc it, returns the reponse immediately and terminated. Is there any other way to run lambda in async or I am doing something wrong ?

like image 940
Vikram Singh Shekhawat Avatar asked Apr 23 '18 08:04

Vikram Singh Shekhawat


People also ask

Can Lambda return string?

A free variable can be a constant or a variable defined in the enclosing scope of the function. The lambda function assigned to full_name takes two arguments and returns a string interpolating the two parameters first and last .

Can Lambda continue after returning response?

No, this isn't possible.

Can Lambda run multiple threads?

Lambda supports Python 2.7 and Python 3.6, both of which have multiprocessing and threading modules.

Can Lambda return 2 values?

That's not more than one return, it's not even a single return with multiple values. It's one return with one value (which happens to be a tuple).

How to create a thread using lambda expression in Java?

In the below program, we can create a thread by implementing the Runnable interface using lamda expression. While using the lambda expressions, we can skip the new Runnable () and run () method because the compiler knows that Thread object takes a Runnable object and that contains only one method run () that takes no argument.

How lambda expression works with the return statements?

As you know Lambda expression supported the Functional interface. It creates a reference for the interface and provides the body with the abstract method. We have seen many examples with a lambda expression. Now we will discuss how Lambda expression works with the return statements.

Can’t use Lambdas in Java 8?

If you can’t use Java 8 lambdas — or don’t want to — here’s the pre-lambda thread syntax using a Runnable: Here’s the old Thread syntax, using the anonymous class approach: You can also create a class to extend a Thread and then run it, like this:

What is the Java 8 lambda syntax for a runnable?

First, here’s the Java 8 lambda syntax for a Runnable, where I create a Runnable and pass it to a Thread: Runnable runnable = () -> { // your code here ... }; Thread t = new Thread (runnable); t.start (); Here’s the Java 8 Thread lambda syntax (without a Runnable ):


1 Answers

Is there any other way to run lambda in async or I am doing something wrong?

Lambda functions can be invoked using the synchronous "request/response" model, or the asynchronous "event" model. These two invocation types ultimately have nothing to do with the way the code inside the Lambda function works.

  • RequestResponse (synchronous) - the Lambda API call doesn't return until the function returns a response or error or the invocation runs past the allowed time; failures must be retried by invoking the again, yourself.
  • Event (asynchronous) - the Lambda API call returns immediately, and the function runs to completion, with its output discarded; failures are retried twice.

It seems as if you are invoking the function using request/response, but then trying to continue to work after returning a response, which overlooks an intrinsic part of the design of the Lambda service. Each invocation runs until it is finished, and then it returns a response. When invoked asynchronously, the response is discarded, but that response is what signals the end of execution, that the function has either succeeded or failed at its task. Lambda stops the billing timer and freezes or destroys the container at that point. A synchronous response cannot be returned earlier than the end of execution.

Lambda runs only one concurrent invocation within each container, so you don't need multiple threads unless an individual invocation is doing more than one thing that needs its own thread.

If you need output from the Lambda function and more processing to continue in the background, then you need function1 invoked request/response, and function1 needs to call the Lambda service API and invoke function2 asynchronously.

In this case, you don't appear to actually need the output, so you could invoke this function asynchronously from API Gateway by adding the X-Amz-Invocation-Type:Event header to the integration request -- but your function needs to "block" until it is done -- that is, it should not return a response until it is finished processing (and remember, the response will be discarded). This may seem counter-intuitive, until you remember that each parallel invocation runs in its own container.

like image 156
Michael - sqlbot Avatar answered Oct 08 '22 22:10

Michael - sqlbot