我正在尝试创建一个网站,用户可以提交Python代码,它会被发送到我的服务器执行,然后将结果发送回客户端。目前我正在使用NodeJs服务器,并需要从那里运行Python代码。为此,我正在使用Python-shell,如下所示:
const runPy = async (code) => {
const options = {
mode: 'text',
pythonOptions: ['-u'],
scriptPath: path.join(__dirname, '../'),
args: [code],
};
const result = await PythonShell.run('script.py', options, (err, results) => {
if (err) throw err;
return results; <----- HOW DO I RETURN THIS
});
console.log(result.stdout);
return result;
};
我知道我可以在
PythonShell.run()
中使用console.log()
来打印结果,但是有没有一种方法可以将我的runPy
函数的结果返回,以便进行操作并发送回客户端?