NodeJ调用Python脚本
我有这段代码。如果是真的,它应该调用我的python脚本 问题是:脚本无法执行。我正在使用Heroku,我的构建包中已经有了nodejs和pythonNodeJ调用Python脚本,python,node.js,heroku,Python,Node.js,Heroku,我有这段代码。如果是真的,它应该调用我的python脚本 问题是:脚本无法执行。我正在使用Heroku,我的构建包中已经有了nodejs和python const express = require('express'); const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); let {PythonShel
const express = require('express');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
let {PythonShell} = require('python-shell');
app.post('/api/apipath', (req, res) => {
if (currentPhase == phaseId){
PythonShell.run('./src/main.py', null, function (err){
if (err) throw err;
console.log('Finish');
});
res.end();
}else{
res.end();
}
}
注意:我已经强制条件为真,但它不起作用。脚本没有被调用我不熟悉
pythonshell
,但是您可以使用节点的exec
来运行脚本
const{exec}=require('child_process')
函数runPythonScript(脚本,runFile=false){
console.log(`Run?${runFile}`)
如果(运行文件){
exec(`python${script}`,(error,stdout,stderr)=>{
如果(错误){
console.error(`exec error:${error}`)
返回
}
log(`stdout:${stdout}`)
console.error(`stderr:${stderr}`)
})
}
}
运行代码:
//这将运行
runPythonScript('./src/main.py',1==1)
//这不会运行
runPythonScript('./src/main.py',0)
//这将运行,因为currentPhase和phaseId都是空变量
无功电流相位
runPythonScript('./src/main.py',currentPhase===phaseId)
CLI 如果要从命令行定义
currentPhase
和phaseId
,请为添加此代码
function init() {
const [, , ...args] = process.argv
var currentPhase, phaseId
currentPhase = args[args.indexOf('--currentPhase') - 1]
phaseId = args[args.indexOf('--phaseId') - 1]
runPythonScript('./program.py', currentPhase === phaseId)
}
init()
从CLI,您可以运行
#将currentPhase设置为99,将phaseId设置为98(不运行)
node lib/filename-to-runPythonScript.js 99--currentPhase 98--phaseId
#将currentPhase和phaseId设置为123(不运行)
节点lib/filename-to-runPythonScript.js 123--currentPhase 123--phaseId
当你说“不执行”时,会发生什么?代码是否真的试图执行脚本,或者currentPhase
是否总是与phaseId
不同?我已经强制条件为true,但它不起作用。没有调用脚本