我目前正在基于 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/