Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在node.js中切换python脚本_Javascript_Python_Node.js - Fatal编程技术网

Javascript 在node.js中切换python脚本

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

我正在使用节点服务器来运行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,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 ...
    })
}