小编Lad*_*aku的帖子

如何从函数中的子进程返回值?

我在 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)

javascript python child-process node.js botframework

3
推荐指数
1
解决办法
4212
查看次数