在AWS Lambda上使用child_process生成的python脚本 [英] using child_process spawn on aws lambda for a python script

查看:107
本文介绍了在AWS Lambda上使用child_process生成的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过child_process.spawn系统使用我的javascript文件运行python脚本,但是它似乎从未在aws lambda上运行。

I'm trying to run a python script using my javascript file through the child_process.spawn system, but it seems to never run on the aws lambda.

相关代码是:

getEntities: function (){
    var spawn = require('child_process').spawn;
    var py = spawn('python', ['mainPythonFile.py']);
    var outputString = "starting string";

    console.log("BEFORE ANY INPUT");
    py.stdout.on('data', function (data) {
        console.log("----Getting information from the python script!---");
        outputString += data.toString();
        console.log(outputString);
    });

    py.stdout.on('end', function (){
        console.log("===hello from the end call in python files===");
        console.log("My output : " + outputString);
    });
    console.log("NO INPUT CHANGED??");

    return outputString;

}

这些文件与文件夹结构(表面级别)。

正在运行的python文件非常简单,仅包含一些打印语句:

The python file being run is quite simple and only contains a few print statements:

MainPythonFile:

import sys;
print("Hello There");
print("My name is Waffles");
print("Potato Waffles");
sys.stdout.flush()

输出从aws服务获得的是这样的:

The output I get from the aws service is this :

BEFORE ANY INPUT
NO INPUT CHANGED??
starting string

我在尝试访问python文件时尝试了不同的路径,例如作为
* mainPythonFile.py ./ mainPythonFile.py 等。

I've tried different paths on trying to access the python file, such as *mainPythonFile.py ./mainPythonFile.py etc.

我认为代码似乎可以在我的本地计算机上运行,但尝试将其运行在我无法理解的AWS上却有些微妙。

I think the code seems to be fine as this works on my local machine, but there's a subtlety of trying to make it run on AWS that I cannot understand.

如果需要,我可以提供其他任何信息。

I can provide any other info if need be.

注意: getEntities函数正在由另一个节点调用.js文件,但是我将代码移到了调用函数,却得到了相同的结果。

NOTE: the "getEntities" function is being called by another node.js file, but I moved the code to the calling function, I get the same result.

推荐答案

由于异步的性质正如Chris所解释的那样,该函数在实际调用生成的线程中的 end之前到达了 return语句。

Due to the asynchronous nature of JS, as explained by Chris, the function reaches the "return" statement before the "end" in the spawned thread is actually called.

这意味着代码从未得到

我更改了函数调用以接受回调,然后

I changed my function calls to take in a callback, that would then respond when the program had replied with the information.

我的新功能对此稍作更改(没有打印):

My new function is slightly changed to this (without the prints):

getEntities: function(callbackFunction, that){
var spawn = require('child_process').spawn;
var py = spawn('python', ['mainPythonFile.py']);
var outputString = "starting string";

py.stdout.on('data', function (data) {
    outputString += data.toString();
});
// that = "this == alexa" that's passed in as input.
py.stdout.on('end', function (){
    callbackFunction(outputString, that);
});

调用此函数的函数如下:

The function that called this function is now as follows :

HelperFunctions.getEntities(function(returnString,that){
  that.response.speak(returnString);
  that.emit(':responseReady');
}, this);

我敢肯定有一种更漂亮的方法可以做到这一点,但目前看来这是可行的。感谢ChrisG

I'm sure there's a prettier way to do this, but this seems to be working for now. Thanks to ChrisG

这篇关于在AWS Lambda上使用child_process生成的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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