javascript - 如何在 Node.js 中终止 Python-Shell 模块进程?

标签 javascript python node.js

我目前正在基于 Node 的 Web 界面中使用 python-shell 模块。我遇到的问题主要是语法上的。下面的代码显示了 python 脚本的生成。

var PythonShell = require('python-shell');
PythonShell.run('my_script.py' function (err) {
    if (err) throw err;
    console.log('finished');
}):

这只是 here 中的示例脚本。我如何将其与 Node 关联起来

var procc = require('child_process'.spawn('mongod');
procc.kill('SIGINT');

文档指出 PythonShell 实例具有以下属性:

childProcess:通过child_process.spawn创建的流程实例

但是我该如何真正使用它呢?对于这个特定的模块似乎缺乏示例

最佳答案

例如 -

var python_process;

router.get('/start_python', function(req, res) {
    const {PythonShell} = require("python-shell");

var options = {
    pythonPath:'local python path'
  }
    var pyshell = new PythonShell('general.py');

    pyshell.end(function (err) {
        if (err) {
            console.log(err);
        }
    });
    python_process = pyshell.childProcess;

    res.send('Started.');
});

router.get('/stop_python', function(req, res) {
   python_process.kill('SIGINT');
   res.send('Stopped');
});

关于javascript - 如何在 Node.js 中终止 Python-Shell 模块进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45758671/

相关文章:

javascript - id未知时获取textarea的值

python - 值错误 : Input arrays should have the same number of samples as target arrays. 找到 166 个输入样本和 4 个目标样本

python - 我的直肠没有更新

Python importlib import_module 模块的相对导入

javascript - 路由获取参数和应用逻辑

javascript - 如何从数组 1 中删除项目并将项目推送到数组 2

javascript - 如何使 slider 在 iPad 上可触摸?

Javascript - 并行调用两个异步函数并将两个结果传递给第三个函数

javascript - 仅当链中的所有 'then' 都已解析时才解析 Promise

javascript - 将背景颜色更改 [css] 链接到背景图像更改 [javascript]