为什么AWS Lambda函数在执行回调函数之前完成? [英] Why Does AWS Lambda function finishes before callback function is executed?

查看:136
本文介绍了为什么AWS Lambda函数在执行回调函数之前完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,以从音频文件中获取成绩单.音频文件的格式为flac.我正在使用AWS Lambda,并已在node中编写了代码.另外,我正在使用IBM Speech to text服务,并使用它们给出的基本示例代码,可以在

I am working on a project to get the transcript out of an audio file. Audio files are of the format flac. I am using AWS Lambda and have written the code in node. Also, I am using IBM Speech to text service and using the basic example code given by them which can be found here. The problem is that my lambda function finishes before running these functions.

我正在从s3下载文件并将其存储在本地(工作正常).之后,我尝试将同一文件传递给IBM Speech to Text SDK,后者应将音频文件的笔录返回到本地存储

I am downloading a file from s3 and storing it locally(which is working fine). After that, I am trying to pass the same file to IBM Speech to Text SDK which should return the transcripts of the audio file to the local storage

这是代码:

const downloadFile = function (url1, dest, cb) {
    const file = fs.createWriteStream(dest);
    https.get(url1, function (res) {
        //res.setEncoding('binary');
        res.pipe(file);
        file.on('finish', function () {
            const stats = fs.statSync(dest);
            const fileSizeInBytes = stats.size;
        //Convert the file size to megabytes (optional)
            const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
            console.log(fileSizeInMegabytes);
            file.close();
            RunIBMWatson(dest);
            callback(null,"Nice");
        });
    });
};
function RunIBMWatson(dest){
    console.log(dest);
    console.log("I am here");

    const recognizeStream = speech_to_text.createRecognizeStream(params);
    fs.createReadStream(dest).pipe(recognizeStream);
    recognizeStream.pipe(fs.createWriteStream('/tmp/transcription.txt'));
    recognizeStream.setEncoding('utf8');
    recognizeStream.on('results', function(event) { onEvent('Results:', event); });
    recognizeStream.on('data', function(event) { onEvent('Data:', event); });
    recognizeStream.on('error', function(event) { onEvent('Error:', event); });
    recognizeStream.on('close', function(event) { onEvent('Close:', event); });
    recognizeStream.on('speaker_labels', function(event) { onEvent('Speaker_Labels:', event); });

    function onEvent(name, event) {
      console.log("I am in onEvent");
      if (name === 'data'){
        console.log(event);
      }

这是我从AWS Lambda获得的功能日志:

and Here is the function logs that I get from AWS Lambda:

2018-03-05 03:31:53.585 54.093469
2018-03-05 03:31:53.588 /tmp/sample.flac
2018-03-05 03:31:53.588 I am here

我是AWS Lambda和Node的入门者.因此,如果有人能指出我犯的错误.

I am a starter in both AWS Lambda and Node. So if anyone can point out the mistake I am making.

推荐答案

是的RunIBMWatson是异步函数,因为存在文件IO,所以因为您不等待该函数的结果返回-回调为已执行,从而结束了lambda的执行.

Yea RunIBMWatson is an asynchronous function because of the file IO, so because you're not waiting for the result from that function to return - the callback is executed thus ending the execution of your lambda.

RunIBMWatson的逻辑包装在Promise中,一旦获得所有数据并将其写入该抄录文件,请解析该函数​​. MDN:承诺

Wrap the logic of RunIBMWatson in a Promise and once all the data is obtained and written to that transcript file - resolve the function. MDN: Promises

const downloadFile = function (url1, dest, cb) {

  ...
  console.log(fileSizeInMegabytes);
  file.close();
  return RunIBMWatson(dest)
  .then(() => { // return data from promise resolve can be back and accessible as params
    callback(null,"Nice");
  }
}

function RunIBMWatson(dest){

  return new Promise((resolve, reject) => {

    const rStream = speech_to_text.createRecognizeStream(params);
    fs.createReadStream(dest).pipe(rStream);

    rStream.pipe(fs.createWriteStream('/tmp/transcription.txt'));
    rStream.setEncoding('utf8');
    rStream.on('results', function(event) { onEvent('Results:', event); });
    rStream.on('data', function(event) { onEvent('Data:', event); });
    rStream.on('error', function(event) { onEvent('Error:', event); });
    rStream.on('close', function(event) { onEvent('Close:', event); });
    rStream.on('speaker_labels', function(event) { onEvent('Speaker_Labels:', event); });

    function onEvent(name, event) {
      console.log("I am in onEvent");
      if (name === 'data'){ // the data 
        resolve(); // you can return data too here
      }
    }
  })
}

希望这会有所帮助

这篇关于为什么AWS Lambda函数在执行回调函数之前完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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