JayJay-K
JayJay-K

Reputation: 11

In case spawn of electron, I can't see output of process in realtime

I'm beginer of electron, and I learned I can execute external program with spawn function. But, when I execute external program, I can't see output data of the program immediately.

This is my external python program. You can see it's a simple program printing "Tick" and saving it to the file. And it works fine, when I execute it manually. I can see the "Tick" every each second.

import time

while True:
    with open("tick.txt", "a") as f:
        f.write("Tack")
        f.close()
    print("Tick")
    time.sleep(1)

But, when I execute it by electron spawn() with following code, I can not see "Tick" on the VS-Code terminal.

function ExecuteTick()
{
    try {
        console.log("Executing Tick");
        const child_process = spawn(
            "python",
            [
                "../converter/tick.py"
            ]);

        child_process.stdout.on('data', (data) => {
            console.log(data.toString());
        });
        child_process.stderr.on('data', (data) => {
            console.error("Tick-Error: " + data);
        });
        child_process.on('close', (code) => {
            console.log("Tick-End: " + code);
        });
    }catch(err){
        console.error("Error on executing Tick - " + err);
    }
}

I believe this Tick program run properly in the background. Because I can see "Tack" from the 'tick.txt' file.

So, my problem is that, When I execute Tick program by electron spawn(), I can not see stdout from the Tick program in realtime, even though it runs properly.

Could you give me some advice for this problem? Thanks.

Upvotes: 0

Views: 26

Answers (0)

Related Questions