Reading stdout of a spawned process waiting for input

I have a Swift program that writes information to stdoutwhile waiting \n(which completes execution). It requests input immediately after start-up and registers information 1 ~ 2 seconds after:

fetchAndLogDataInBackground(); // will print some data in ~1 sec

readLine();

I spawning / execFilewith the following:

const spawn = require('child_process').spawn;

const process = spawn('swift/main');
process.stdout.on('data', data => console.log(data.toString()));

setTimeout(() => {
    process.stdin.write('\n');
}, 5000);

I expected to see the "live" logs, as I use on('data'), but they are processed only afterprocess.stdin.write('\n');

Is there a way to get live data?

PS: if I run the Swift program in terminal ( swift/main), it works as expected.

+4
source share
1 answer

stdout, stdout Swift.

:

fflush(__stdoutp)

:

setbuf(__stdoutp, nil);
+2

Source: https://habr.com/ru/post/1653253/


All Articles