AWS lambda和Java并发 [英] AWS lambda and Java concurrency

查看:109
本文介绍了AWS lambda和Java并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,AWS lambda 可以重用早期创建的处理程序对象,而且确实如此(参见常见问题解答):

It is known that AWS lambda may reuse early created objects of handlers, and it really does it (see FAQ):


问:AWS Lambda会重用函数实例吗?

为了提高性能,AWS Lambda可以选择保留您的函数
的实例并重复使用它来提供后续请求,而不是
创建新副本。你的代码不应该假设这总是
发生。

To improve performance, AWS Lambda may choose to retain an instance of your function and reuse it to serve a subsequent request, rather than creating a new copy. Your code should not assume that this will always happen.






问题是关于 Java 并发。如果我有处理程序的类,请说:


The question is regarding to Java concurrency. If I have class for handler, say:

public class MyHandler {
    private Foo foo;
    public void handler(Map<String,String> request, Context context) {
       ...
    }
}

所以,这里是否可以线程安全地访问和使用对象变量 foo

so, will it be thread-safe to access to and work with object variable foo here or not?

换句话说:AWS lambda可以同时为不同的呼叫使用相同的对象吗?

In other words: may AWS lambda use same object concurrently for different calls?

编辑我的函数是在基于事件的源上处理的,特别是它由API网关方法调用。

EDIT My function is processed on event based source, particular it is invoked by API Gateway method.

EDIT-2 当您想要为外部资源实现某种连接池时,这类问题会上升,所以我想保持连接将外部资源作为对象变量。它实际上可以正常工作,但我害怕并发问题。

EDIT-2 Such kind of question rises when you want to implement some kind of connection pool to external resources, so I want to keep connection to external resource as object variable. It actually works as desired, but I'm afraid of concurrency problems.

EDIT-3 更具体地说,我想知道: AWS lambda的处理程序实例是否可以共享公共堆(内存)?我必须指定这个额外的细节,以防止回答列出有关java线程安全对象的明显和常见的事情。

EDIT-3 More specifically I'm wondering: can instances of handlers of AWS lambda share common heap (memory) or not? I have to specify this additional detail in order to prevent answers with listing of obvious and common-known things about java thread-safe objects.

推荐答案


可能AWS lambda同时为不同的调用使用相同的对象吗?

May AWS lambda use same object concurrently for different calls?

AWS lambda的处理程序实例可以共享公共堆(内存)或不是吗?

Can instances of handlers of AWS lambda share common heap (memory) or not?

强烈,明确的NO。 AWS Lambda处理程序的实例甚至无法共享文件(在 / tmp 中)。

A strong, definite NO. Instances of handlers of AWS Lambda cannot even share files (in /tmp).

AWS Lambda容器可能 not 可以重用两个或多个并发的Lambda函数调用,因为这会破坏隔离要求:

An AWS Lambda container may not be reused for two or more concurrently existing invocations of a Lambda function, since that would break the isolation requirement:

问:AWS Lambda如何隔离我的代码?


每个AWS Lambda函数在其自己的隔离环境中运行 ,具有自己的资源和文件系统视图。

Each AWS Lambda function runs in its own isolated environment, with its own resources and file system view.

AWS Lambda如何运行我的代码?容器模型部分lambda / latest / dg / lambda-introduction-function.htmlrel =noreferrer> lambda函数如何工作状态:

The section "How Does AWS Lambda Run My Code? The Container Model" in the official description of how lambda functions work states:


执行Lambda函数后,AWS Lambda会在一段时间内维护
容器预期另一个Lambda函数
调用。实际上,服务在
Lambda函数完成后冻结容器,并解冻容器以便重用,如果AWS
Lambda选择在Lambda函数再次调用
时重用容器。此容器重用方法具有以下
含义:

After a Lambda function is executed, AWS Lambda maintains the container for some time in anticipation of another Lambda function invocation. In effect, the service freezes the container after a Lambda function completes, and thaws the container for reuse, if AWS Lambda chooses to reuse the container when the Lambda function is invoked again. This container reuse approach has the following implications:


  • Lambda函数代码中的任何声明仍保持初始化状态,
    在再次调用函数时提供额外的优化。
    例如,如果您的Lambda函数建立数据库
    连接,而不是重新建立连接,则在后续调用中使用原始
    连接。您可以在
    中添加逻辑代码,以便在创建连接之前检查连接是否已存在。

每个容器都提供一些磁盘空间在/ tmp目录中。当容器被冻结时,
目录内容仍然存在,提供可用于多次调用的
瞬态缓存。您可以添加
额外代码来检查缓存是否包含您存储的数据。

Each container provides some disk space in the /tmp directory. The directory content remains when the container is frozen, providing transient cache that can be used for multiple invocations. You can add extra code to check if the cache has the data that you stored.

Lambda函数启动的后台进程或回调$ b如果AWS Lambda
选择重用容器,则在函数结束时未完成的$ b。在代码退出之前,您应确保代码中的任何后台
进程或回调(如果是Node.js)都是完整的

Background processes or callbacks initiated by your Lambda function that did not complete when the function ended resume if AWS Lambda chooses to reuse the container. You should make sure any background processes or callbacks (in case of Node.js) in your code are complete before the code exits.

正如您所看到的,在尝试利用容器重用时,Lambda函数的多个并发调用之间绝对没有关于竞争条件的警告。唯一的注意事项是不要依赖它!。

As you can see, there is absolutely no warning about race conditions between multiple concurrent invocations of a Lambda function when trying to take advantage of container reuse. The only note is "don't rely on it!".

这篇关于AWS lambda和Java并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆