I'm trying to create an Electron app that executes a Python script but I cannot retrieve messages from python before the end of the script.
This is the code at the beginning of the file 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);
This is the python script:
import sys
import time
print("START")
time.sleep(10)
print("After 10 seconds")
This is the shell:
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