我在nodeJS和Python之间的通信遇到了一些问题。
但是,结果不会存储。相反,结果会在shell运行之前打印出来。为什么会这样呢?如何将python输出存储到对象中?
在py脚本中编写一个临时JSON文件是不是更好?
下面是我的代码:
var pyshell = require('python-shell');
var result;
pyshell.PythonShell.run('suggestionWSong.py', null, function (err, results) {
if (err) throw err;
console.log('hello.py finished.');
console.log(results[0]);
result = results[0];
console.log(result);
});
console.log("AA");
console.log(result);
这是我的输出:
AA
undefined
hello.py finished.
{'songs': [{'songName': 'song1New', 'author': 'auth1', 'features': {'bpm': 100, 'key': 'A', 'scale': 'Minor'}}, {'songName': 'song2New', 'author': 'auth2', 'features': {'bpm': 200, 'key': 'B', 'scale': 'Major'}}, {'songName': 'song3New', 'author': 'auth3', 'features': {'bpm': 300, 'key': 'C', 'scale': 'Minor'}}]}
{'songs': [{'songName': 'song1New', 'author': 'auth1', 'features': {'bpm': 100, 'key': 'A', 'scale': 'Minor'}}, {'songName': 'song2New', 'author': 'auth2', 'features': {'bpm': 200, 'key': 'B', 'scale': 'Major'}}, {'songName': 'song3New', 'author': 'auth3', 'features': {'bpm': 300, 'key': 'C', 'scale': 'Minor'}}]} 最终,我想要做的是在一个叫做Python的电子应用程序中实现TS函数。我使用tsc将TS编译成JS,然后在Electron上运行。我不确定是应该将结果发送回TS类,还是应该编写一个我可以根据需要读取的JSON文件。这些信息将是歌曲库数据,所以我预计5-10首歌曲&它们的特征。
发布于 2021-02-01 16:26:30
pyshell.PythonShell.run是一个异步函数。这意味着它需要时间来执行,因此您的脚本将执行它下面的任何代码,而不是等待它
所以这就像这样做
var pyshell = require('python-shell');
var result;
console.log("AA");
console.log(result);
pyshell.PythonShell.run('suggestionWSong.py', null, function (err, results) {
if (err) throw err;
console.log('hello.py finished.');
console.log(results[0]);
result = results[0];
console.log(result);
});您需要告诉脚本等待函数执行,或者在回调函数中运行您想要的任何东西,但要注意不要陷入Callback Hell
看一下promises或async await语法
你需要这样做:
var pyshell = require('python-shell');
var result;
let myPromise = new Promise((reject, resolve)=>{
pyshell.PythonShell.run('suggestionWSong.py', null, function (err, results) {
if (err) reject(err);
else {
console.log('hello.py finished.');
resolve(results);
}
});
});
result = await myPromise
console.log("AA");
console.log(result);注意:通常你需要一个异步函数来使用await,但是在最新版本的node中,你可以像我一样使用它
发布于 2021-02-02 07:21:47
我对“白死”给我的建议做了一些修改。由于某种原因,我不能让他的等待工作。在任何情况下,以下替代方案对我都有效。
var pyshell = require('python-shell');
var result;
test();
async function test(){
result = await go();
console.log("AA");
console.log(result);
}
async function go(){
return new Promise((resolve, reject)=>{
pyshell.PythonShell.run('suggestionWSong.py', null, function (err, results) {
if (err){
console.log('fail');
reject(err);
}
else {
console.log('hello.py finished.');
console.log(results[0]);
resolve(results[0]);
}
});
});
}
hello.py已完成。{‘歌曲’:{'songName':'song1New','auth1','features':{'bpm':100,'key':'A','scale':'Minor'}},{'songName':'song2New','auth2','features':{'bpm':200,'key':'B','scale':‘重大’}},{'songName':'song3New','author':'auth3','features':{'bpm':300,'key':'C','scale':‘Minor’}} AA {‘歌曲’:{'songName':'song1New','author':'auth1','features':{'bpm':100,'key':'A','scale':'Minor'}},{'songName':'song2New','auth2','features':{'bpm':200,'key':'B','scale':‘大调’}},{'songName':'song3New','auth3','features':{'bpm':300,'key':'C','scale':‘Minor’}}
https://stackoverflow.com/questions/65988913
复制相似问题