NodeModule(CPP) -> Extern C Function,此设置是使用 node-gyp -6ren">
gpt4 book ai didi

c - 使用 Node 和 Node-gyp 在 AWS Lambda 上出现奇怪的 "memory"行为

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:16 26 4
gpt4 key购买 nike

我有一个用 NodeJS 编写的 AWS Lambda,调用过程非常简单

NODEJS -> NodeModule(CPP) -> Extern C Function,此设置是使用 node-gyp 编译的。您可以在此处查看完整代码 https://drive.google.com/open?id=0B-2d-CuY5fkwS3lwdE96R1V6NEk

CPP Node 模块调用 C 中的一个函数,该函数运行一个循环。并递增两个变量,一个在 C 函数的范围内,另一个在 C 代码的主范围内。

当您在本地运行此代码时。循环递增,两个变量都达到 11,正如您运行它所预期的那样。但是当您在 AWS Lambda 中运行相同的代码时,每次调用都会有某种“内存”。并且一般范围内未被重置的变量正在增加,以 11、22、33 等的倍数增加。

重复一遍,这永远不会在本地发生,两个变量始终为 11。你可以通过运行来构建1. node-gyp 清理配置构建2. node app.js(本地运行)

Index.js 适用于 AWS Lambda

我真的无法解释这种行为? Lambda 是否提供某种上下文或某种“内存”或缓存?

我为此制作了一个 Open API 网关。 (随时刷新并查看正在运行的“内存”)。

https://koj2yva6z9.execute-api.us-east-1.amazonaws.com/dev/testLambdaCache

这种行为有时会不一致,有时会重置计数。或者您可以通过上传新的 AWS lambda 代码来重置。

对这种奇怪行为的任何想法表示赞赏。

app.js(用于本地测试)

var addon = require('./build/Release/addon');
console.log(addon.testCache());
console.log(" addon method completed");

index.js(在 lambda 中使用)

console.log('Loading function');

exports.handler = (event, context, callback) => {

var addon = require('./build/Release/addon');
var returnvalue=addon.testCache();
console.log(returnvalue);
console.log(" addon method completed");
callback(null, "success::"+returnvalue);
}

base.cc(C 代码的包装器)

#include <node.h>
#include <iostream>
#include <stdlib.h>
#include<string>
#include<cstring>

using namespace std;

extern "C" char* testCache();

namespace demo {

using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Exception;

void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
cout << "C++ method started\n";
char *returnStrings=NULL;
returnStrings= testCache();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, returnStrings ));
}

void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "testCache", Method);
}

NODE_MODULE(addon, init)

}

decoder.c(运行循环的c代码)

int tmpCounter=0;


char* testCache()
{
int counter=0;
printf("Local counter --> %d Global Counter --> %d\n",counter,tmpCounter);
for(int i=0;i <10; i++)
{
counter = counter +1;
tmpCounter = tmpCounter +1;
//sleep(1);
}
printf("Local counter --> %d Global Counter --> %d\n",counter,tmpCounter);

counter=counter+1;
tmpCounter=tmpCounter+1;

char strCounter[100];
char strTmpCounter[100];
snprintf(strCounter, 16, "%d", counter);

snprintf(strTmpCounter, 16, "%d", tmpCounter);

char *returnString=NULL;
returnString=malloc(1000);

strcat(returnString, "Count:");
strcat(returnString, strCounter);
strcat(returnString, " TmpCount:");
strcat(returnString, strTmpCounter);
strcat(returnString, "\0");
printf("%s\n",returnString);
fflush(stdout);
return returnString;

}

最佳答案

is there some sort of context or some sort of "memory" or caching that is available with Lambda?

我不会说它是“可用的”,因为它是可预测的或一致的,因为您不应该围绕它进行设计,但是是的,存在容器重用。

要查看实际效果:

创建一个 uuid 或随机数,或类似的东西,并将其存储在处理程序外部的全局变量中。然后,在处理程序内部,记录它。您会看到相同的进程或进程组(由 uuid 标识)可能但不一定会处理时间上接近的后续请求。

Let’s say your function finishes, and some time passes, then you call it again. Lambda may create a new container all over again [...]

However, if you haven’t changed the code and not too much time has gone by, Lambda may reuse the previous container. This offers some performance advantages to both parties: Lambda gets to skip the nodejs language initialization, and you get to skip initialization in your code. Files that you wrote to /tmp last time around will still be there if the sandbox gets reused.

https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/

关于c - 使用 Node 和 Node-gyp 在 AWS Lambda 上出现奇怪的 "memory"行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37987271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com