Answers for "node js child_process read stdout"

0

node js child_process read stdout

//Prints stdout of childprocess(here: from spawning a python script) 
// line by line, the rest (like errors) as whole:

const { spawn } = require("child_process");
const { chunksToLinesAsync, chomp } = require('@rauschma/stringio');

const file = require.resolve('./dummyData.py');

async function main() {
    const ls = spawn("python" , [file]);

    await echoStdout(ls.stdout); 

    async function echoStdout(readable) {
        for await (const line of chunksToLinesAsync(readable)) { // (C)
        console.log('LINE: '+chomp(line))
        }
    }

    ls.stdout.on("data", data => {
        console.log(`stdout: ${data}`);
    });
    
    ls.stderr.on("data", data => {
        console.log(`stderr: ${data}`);
    });
    
    ls.on('error', (error) => {
        console.log(`error: ${error.message}`);
    });
    
    ls.on("close", code => {
        console.log(`child process exited with code ${code}`);
    }); 
}

main();
Posted by: Guest on June-24-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language