ค้นหาเว็บไซต์

javascript - Meteor วิธีการรันสคริปต์ Python บนด้านเซิร์ฟเวอร์


ฉันมีแอป Meteor ที่จำเป็นต้องเรียกสคริปต์ Python เพื่อทำงานบางอย่างในพื้นหลัง ฉันจะทำให้มันทำงานได้อย่างไร? ฉันได้ลองใช้ child_process และ exec แล้ว แต่ดูเหมือนว่าจะไม่สามารถทำให้มันทำงานได้ถูกต้อง สคริปต์ควรจะอยู่ที่ไหน?

ขอบคุณ

คำตอบ #1

ฉันมีปัญหาเดียวกันและมันถูกแก้ไขโดยใช้ python-sheel และแพ็กเกจ npm เพื่อรันสคริปต์ Python จาก Node.js ติดตั้งบน meteor :

meteor npm install --save python-shell

มีวิธีใช้งานง่าย ๆ :

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

PythonShell.run('my_script.py', function (err) {
  if (err) throw err;
  console.log('finished');
});

ถ้าคุณต้องการรันพร้อมอาร์กิวเมนต์และตัวเลือก:

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);
});

นี่คือรายละเอียดเกี่ยวกับ python-shell https://github.com/extrabacon/python-shell ขอบคุณ extrabacon :)