nodejs:在 aws lambda 中使用子进程到 运行 python 脚本不起作用
nodejs: Using child process in aws lambda to run python script not working
使用 serverless framework and post 作为指导,我想从 python 脚本打印出一些信息,我在 handler.js
const path = await require('path')
const { spawn } = await require('child_process')
/**
* Run python script, pass in `-u` to not buffer console output
* @return {ChildProcess}
*/
runScript() {
return spawn('python', [
"-u",
path.join(__dirname, 'script.py'),
"--foo", "some value for foo",
]);
}
const subprocess = await runScript()
// print output of script
await subprocess.stdout.on('data', (data) => {
console.log(`data:${data}`);
});
await subprocess.stderr.on('data', (data) => {
console.log(`error:${data}`);
});
await subprocess.stderr.on('close', () => {
console.log("Closed");
});
我也有script.py
#!/usr/bin/python
import sys, getopt, time
def main(argv):
argument = ''
usage = 'usage: script.py -f <sometext>'
# parse incoming arguments
try:
opts, args = getopt.getopt(argv,"hf:",["foo="])
except getopt.GetoptError:
print(usage)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(usage)
sys.exit()
elif opt in ("-f", "--foo"):
argument = arg
# print output
print("Start : %s" % time.ctime())
time.sleep( 5 )
print('Foo is')
time.sleep( 5 )
print(argument)
print("End : %s" % time.ctime())
if __name__ == "__main__":
main(sys.argv[1:])
我希望我的控制台在调用处理函数时显示输出或错误,但它什么也没打印
方法是这样的:
const path = require("path");
const { spawn } = require("child_process");
const runScript = () => {
return spawn("python", [
"-u",
path.join(__dirname, "script.py"),
"--foo",
"some value for foo"
]);
};
const promise = new Promise((resolve, reject) => {
const subprocess = runScript();
subprocess.stdout.on("data", data => {
console.log(`data:${data}`);
});
subprocess.stderr.on("data", data => {
console.log(`error:${data}`);
});
subprocess.on("close", code => {
if (code !== 0) {
reject(code);
} else {
resolve(code);
}
});
});
await promise;
同步函数无需使用 await
,通过用 promise 包装 subprocess
,您可以 await
在其上。
使用 serverless framework and
const path = await require('path')
const { spawn } = await require('child_process')
/**
* Run python script, pass in `-u` to not buffer console output
* @return {ChildProcess}
*/
runScript() {
return spawn('python', [
"-u",
path.join(__dirname, 'script.py'),
"--foo", "some value for foo",
]);
}
const subprocess = await runScript()
// print output of script
await subprocess.stdout.on('data', (data) => {
console.log(`data:${data}`);
});
await subprocess.stderr.on('data', (data) => {
console.log(`error:${data}`);
});
await subprocess.stderr.on('close', () => {
console.log("Closed");
});
我也有script.py
#!/usr/bin/python
import sys, getopt, time
def main(argv):
argument = ''
usage = 'usage: script.py -f <sometext>'
# parse incoming arguments
try:
opts, args = getopt.getopt(argv,"hf:",["foo="])
except getopt.GetoptError:
print(usage)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(usage)
sys.exit()
elif opt in ("-f", "--foo"):
argument = arg
# print output
print("Start : %s" % time.ctime())
time.sleep( 5 )
print('Foo is')
time.sleep( 5 )
print(argument)
print("End : %s" % time.ctime())
if __name__ == "__main__":
main(sys.argv[1:])
我希望我的控制台在调用处理函数时显示输出或错误,但它什么也没打印
方法是这样的:
const path = require("path");
const { spawn } = require("child_process");
const runScript = () => {
return spawn("python", [
"-u",
path.join(__dirname, "script.py"),
"--foo",
"some value for foo"
]);
};
const promise = new Promise((resolve, reject) => {
const subprocess = runScript();
subprocess.stdout.on("data", data => {
console.log(`data:${data}`);
});
subprocess.stderr.on("data", data => {
console.log(`error:${data}`);
});
subprocess.on("close", code => {
if (code !== 0) {
reject(code);
} else {
resolve(code);
}
});
});
await promise;
同步函数无需使用 await
,通过用 promise 包装 subprocess
,您可以 await
在其上。