【发布时间】:2020-07-04 13:35:10
【问题描述】:
我正在尝试创建一个执行 Python 脚本的 Electron 应用程序,但我无法在脚本结束之前从 python 检索消息。
这是main.js文件开头的代码:
const { PythonShell } = require('python-shell');
let pyshell = new PythonShell('app.py');
console.log("MAIN: Script started")
pyshell.on('message', function (message) {
console.log("Message from APP: " + message);
});
// end the input stream and allow the process to exit
pyshell.end(function (err, code, signal) {
if (err) throw err;
console.log('The exit code was: ' + code);
console.log('The exit signal was: ' + signal);
console.log('finished');
});
let seconds = 0
setInterval(function(){
seconds = seconds + 5
console.log("MAIN: " + seconds + " seconds"); }, 5000);
这是python脚本:
import sys
import time
print("START")
time.sleep(10)
print("After 10 seconds")
这是外壳:
MAIN: Script started
MAIN: 5 seconds
MAIN: 10 seconds
Message from APP: START
Message from APP: After 10 seconds
The exit code was: 0
The exit signal was: null
finished
MAIN: 15 seconds
【问题讨论】: