Meteor 如何运行服务器端 python 脚本

Jos*_*hin 5 javascript python meteor

我有一个 Meteor 应用程序,它需要调用一个 python 脚本在后台做一些事情。我怎样才能让它发挥作用?我试过使用 child_process 和 exec,但我似乎无法让它正确执行。脚本应该放在哪里?

谢谢

Pam*_*uda 3

我有同样的问题,它已解决,使用 python-sheel 和 npm 包从 Node.js 安装在 Meteor 上运行 Python 脚本:

meteor npm install --save python-shell
Run Code Online (Sandbox Code Playgroud)

有简单的用法:

var PythonShell = require('python-shell');

PythonShell.run('my_script.py', function (err) {
  if (err) throw err;
  console.log('finished');
});
Run Code Online (Sandbox Code Playgroud)

如果您想使用参数和选项运行:

var PythonShell = require('python-shell');

var options = {
  mode: 'text',
  pythonPath: 'path/to/python',
  pythonOptions: ['-u'],
  scriptPath: 'path/to/my/scripts',
  args: ['value1', 'value2', 'value3']
};

PythonShell.run('my_script.py', options, function (err, results) {
  if (err) throw err;
  // results is an array consisting of messages collected during execution
  console.log('results: %j', results);
});
Run Code Online (Sandbox Code Playgroud)

这里详细介绍了 python-shell https://github.com/extrabacon/python-shell 感谢 extrabacon :)