如何在Node.js中同步运行Python脚本?
我正在Node.js中通过运行以下Python脚本: 输出为:如何在Node.js中同步运行Python脚本?,python,node.js,Python,Node.js,我正在Node.js中通过运行以下Python脚本: 输出为: Now reading data finished 但预期产出是: finished Now reading data Node.js无法同步执行我的Python脚本,它首先执行PythonShell.run函数后面的所有代码,然后执行PythonShell.run。如何首先执行PythonShell。然后运行以下代码?任何帮助都将不胜感激。。。这是紧急情况 由于这是异步的,请添加一个结束回调(可在文档中找到),而不是顶层的指
Now reading data
finished
但预期产出是:
finished
Now reading data
Node.js无法同步执行我的Python脚本,它首先执行
PythonShell.run
函数后面的所有代码,然后执行PythonShell.run
。如何首先执行PythonShell。然后运行以下代码?任何帮助都将不胜感激。。。这是紧急情况 由于这是异步的,请添加一个结束回调(可在文档中找到),而不是顶层的指令
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err) throw err;
console.log ("Now reading data");
});
导出默认值{
数据(){
返回{
}
},
方法:{
调用函数(){
console.log(“正在读取数据”);
},
pyth(){
var PythonShell=require('python-shell');
var vm=this;//将对象(this)保存在变量vm中,以便在pythonshell.run中使用
PythonShell.run('sensor.py',函数(err){
如果(错误)抛出错误;
console.log('finished');
vm.callfunction();
});
}
}
}
我也遇到了同样的问题,并通过使用PythonShell.run
方法来解决它。然后,您只需等待这个承诺,代码就会同步运行
承诺方法:
RunPythonScript: function(scriptPath, args, pythonFile){
let options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: [],
scriptPath: scriptPath,
args: args,
};
return new Promise((resolve,reject) =>{
try{
PythonShell.run(pythonFile, options, function(err, results) {
if (err) {console.log(err);}
// results is an array consisting of messages collected during execution
console.log('results', results);
resolve();
});
}
catch{
console.log('error running python code')
reject();
}
})
},
然后,你等待承诺:
await RunPythonScript(YOURscriptPath,YOURargsScript,YOURpythonFile);
它对我有用:
let {PythonShell} = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: [],
scriptPath: '',
args: []
};
async function runTest()
{
const { success, err = '', results } = await new Promise(
(resolve, reject) =>
{
PythonShell.run('hello.py', options,
function (err, results)
{
if (err)
{
reject({ success: false, err });
}
console.log('PythonShell results: %j', results);
resolve({ success: true, results });
}
);
}
);
console.log("python call ends");
if (! success)
{
console.log("Test Error: " + err);
return;
}
console.log("The result is: " + results);
// My code here
console.log("end runTest()");
}
console.log('start ...');
runTest();
console.log('... end main');
结果是:
start ...
... end main
PythonShell results: ["Hello World!"]
python call ends
The result is: Hello World!
end runTest()
node.js的主要设计目标是异步运行任务。你能解释一下为什么它必须是异步的吗?实际上,我使用的是一个颜色检测器传感器。颜色检测由python脚本完成。根据检测到的颜色,我必须使用node.js进行一些其他计算。我正在处理一个大项目,颜色检测是我任务的一部分,使用python检测颜色非常容易,但其他计算需要使用node.jsI来完成。我尝试过这样做:如果我在“pyshell.end(){}”函数之后还有代码,那么程序首先执行以下代码,然后执行“pyshell.end(){}' . 但是我需要先执行'pyshell.end(){}',然后执行下面的代码我想你误解了Klaus D.一开始所说的:这是一个异步系统。“以下代码”必须位于pyshell.end
回调中。任何东西都不能阻止主程序。只需在图形界面中添加一个按钮。当有人单击回调时,您必须连接回调以作出响应,但主循环仍然是空闲的。
let {PythonShell} = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: [],
scriptPath: '',
args: []
};
async function runTest()
{
const { success, err = '', results } = await new Promise(
(resolve, reject) =>
{
PythonShell.run('hello.py', options,
function (err, results)
{
if (err)
{
reject({ success: false, err });
}
console.log('PythonShell results: %j', results);
resolve({ success: true, results });
}
);
}
);
console.log("python call ends");
if (! success)
{
console.log("Test Error: " + err);
return;
}
console.log("The result is: " + results);
// My code here
console.log("end runTest()");
}
console.log('start ...');
runTest();
console.log('... end main');
start ...
... end main
PythonShell results: ["Hello World!"]
python call ends
The result is: Hello World!
end runTest()