如果调用者返回太早,则不执行异步AWS Lambda [英] Async AWS Lambda not executed if caller returns too early

查看:226
本文介绍了如果调用者返回太早,则不执行异步AWS Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个Lambda函数中的内部调用异步lambda函数,并且我发现如果调用函数退出得太快,它将无法执行.

I'm trying to call an async lambda function from within another lambda function, and I'm finding that it doesn't get executed if the calling function exits too quickly.

换句话说,以下内容永远都行不通. LambdaFunction2永远不会被调用.

In other words, the following doesn't ever work. LambdaFunction2 never gets called.

function lambdaFunction1(event, context) {
  callLambdaFunction2();
  context.done(null);
}

但是到目前为止,在LambdaFunction1退出之前添加一个小的延迟确实可以正常工作:

But adding a small delay before LambdaFunction1 exits does tend to work so far:

function lambdaFunction1(event, context) {
  callLambdaFunction2();
  setTimeout(
    function() {
     context.done(null);
    }, 500
  );
}

让我担心的是,等待500毫秒是一个相当随意的幻数.有没有人遇到过类似的问题并找到了更原则的修复方法?

What concerns me is that waiting 500ms is a rather arbitrary magic number. Has anyone encountered a similar problem and found a more principled fix?

推荐答案

callLambdaFunction2()可能在context.done(null)导致处理程序退出之前未完成.

callLambdaFunction2() probably does not complete before context.done(null) causes handler to exit.

要解决此问题,您需要调用context.done作为回调.例如:

To fix this you would need to call context.done as a callback. For example:

lambda.invoke({
    FunctionName: "functionName",
    InvocationType: 'Event',
    Payload: JSON.stringify(event)
}, function(err, data) {
    if (err) return context.done(err, null);
    return context.done(null, data);
});

如果这不是解决方案,您能否说明如何实现callLambdaFunction2?

If this is not the solution, can you show how you have implemented callLambdaFunction2?

这篇关于如果调用者返回太早,则不执行异步AWS Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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