I tried a fairly simple script.
import time
print("Hello World")
time.sleep(5)
This program does exactly what it appears to be doing. It prints Hello World
, waits for five seconds and then exits.
However, things start getting weird when i invoked this with node.
const spawn = require('child_process').spawn;
const process = spawn ('python3', ['./testIn.py']);
process.stdout.on('data', data => {
console.log(data.toString());
});
process.on('close', code => {
console.log(`child process close all stdio with code ${code}`);
});
What happens now is that the program waits for five seconds, then prints Hello World
and then exits immediately after the print.
It's like the order of the two commands is reversed, first it runs the sleep()
and the the print()
.
Does anyone have a clue on why this weird behaviour occurs?