Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

javascript - 如何从子过程中的函数返回值?(How to return value from a child process in a function?)

I spawn a python child process in javascript (for coding a Bot with Microsoft Botframework) like this:

(我在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: ${code}`);
        // "return" is probably wrong, what should be done instead?
        return output;
        }
    });
}

I want output to be returned as the value of the function searchForRelevanceDoc() .

(我希望将output作为函数searchForRelevanceDoc()的值searchForRelevanceDoc() 。)

How can this be done?

(如何才能做到这一点?)

I am not able to use await context.sendActivity(output) instead of the return statement.

(我不能使用await context.sendActivity(output)而不是return语句。)

Error message:

(错误信息:)

TypeError: Cannot perform 'get' on a proxy that has been revoked

Function searchForRelevanceDoc is called like so:

(函数searchForRelevanceDoc的调用方式如下:)

//in bot.js
const pysearch = require('../bboti/python-search');

class MyBot {

    // constructor...

    async onTurn(context) {
        // ...
        var search_engine_answer = pysearch.searchForRelevantDoc(context);
        context.sendActivity(search_engine_answer)
        // ...
    }
}

  ask by Lady_Hangaku translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@Marcos Casagrande's answer is more beautiful, I just wanted to add the promise solution as well.

(@Marcos Casagrande的答案更漂亮,我也想添加promise解决方案。)

You can just return a new Promise and wrap the stdout.on inside the promise.

(您可以返回一个new Promise并将stdout.on包装在promise中。)

async function searchForRelevantDoc (context) {
...
    return new Promise((res, rej) => {
        py.stdout.on('end', async function(code){
            console.log('output: ' + output);
            console.log(`Exit code is: ${code}`);
            // "return" is probably wrong, what should be done instead?
            res(output);
        })
    });
}

Promises hot-load so whenever you declare a promise the function will start to run.

(承诺hot-load因此每当您声明promise时,该函数就会开始运行。)

So basically the event will be attached.

(因此,基本上该事件将被附加。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...