Javascript 在node.js中切换python脚本
我正在使用节点服务器来运行python脚本。我正在使用pythonshell包来运行它们,这很好。这里是我正在运行的Javascript 在node.js中切换python脚本,javascript,python,node.js,Javascript,Python,Node.js,我正在使用节点服务器来运行python脚本。我正在使用pythonshell包来运行它们,这很好。这里是我正在运行的server.js文件: app.get('/start/:fileName', function(req, res) { let fileName = req.params.fileName; console.log(fileName) PythonShell.run(`./python_scripts/${fileName}`, function (err,re
server.js
文件:
app.get('/start/:fileName', function(req, res) {
let fileName = req.params.fileName;
console.log(fileName)
PythonShell.run(`./python_scripts/${fileName}`, function (err,results) {
console.log('results ',results);
if (err) throw err;
console.log('finished');
});
});
在另一个文件main.js
中,我有以下命令:
function fetchStartEndpoint(fileName) {
fetch(`/start/${fileName}`)
.catch(error => console.log(error));
}``
单击按钮时,我调用fetchStartEndpoint
这一切都很好,但我想使用多个按钮来运行多个脚本。当我单击按钮时,脚本将运行,但当我单击其他按钮以运行其他脚本时,第一个脚本将继续运行。我希望它结束并运行第二个。除了重新启动服务器外,是否有其他方法覆盖初始脚本并运行第二个脚本?我正在快速浏览上的自述文件,在我看来,在shell实例上调用
end
就是停止运行脚本的方法:
运行(脚本、选项、回调)
此方法还返回PythonShell实例
.end(回调)
关闭stdin流,允许Python脚本完成并退出。当进程终止时,将调用可选回调
基于这一点,我认为这是可行的:
// First button pressed ...
var shell = PythonShell.run('script.py', function (err, results) {
// script finished
shell = null
});
// Second button pressed ...
if (shell) {
shell.end(function() {
// start second script ...
})
}