我在 javascript 中生成一个 python 子进程(用于使用 Microsoft Botframework 编写 Bot),如下所示:
async function searchForRelevantDoc (context) {
var msg = context.activity.text;
var spawn = require('child_process').spawn,
py = spawn('python', ['../path/to/file.py', msg]),
output = '';
py.stdin.setEncoding = 'utf-8';
py.stdout.on('data',
(data) => {
output += data.toString();
console.log('output was generated: ' + output);
});
// Handle error output
py.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
console.log('error:' + data);
});
py.stdout.on('end', async function(code){
console.log('output: ' + output);
console.log(`Exit code is: …Run Code Online (Sandbox Code Playgroud)