Handling child process stdout in real time

2,013 views
Skip to first unread message

manimal45

unread,
Aug 23, 2010, 4:40:46 AM8/23/10
to nodejs
Hi,

Is it possible to read a child process stdout (or stderr), in real
time.
It seems that on('data') event is triggered only when the process is
done, am I right ?

Felix Geisendörfer

unread,
Aug 23, 2010, 5:19:54 AM8/23/10
to nodejs
> It seems that on('data') event  is triggered only when the process is
> done, am I right ?

No, 'data' events are being fired in "real time". Your command line
tool may however not output anything before it's done doing it's
thing.

--fg

manimal45

unread,
Aug 23, 2010, 8:26:12 AM8/23/10
to nodejs
Well, that's what I believed too, but here's a simple example that
troubles me.

A simple python script, any javascript programmer should be familiar
with:
1) dummycommand.py

import time
def dummy() :
for i in range(0,10) :
print "dummy process", i+1,"/10"
time.sleep(0.2)
if __name__ =='__main__' :
dummy()

>> when you run (>> python dummyscript.py) it, you get this :
dummy process 1 /10
dummy process 2 /10
dummy process 3 /10
dummy process 4 /10
dummy process 5 /10
dummy process 6 /10
dummy process 7 /10
dummy process 8 /10
dummy process 9 /10
dummy process 10 /10


2) Now, the canonical nodejs example from child processes
documentation : test.js
var sys = require('sys'),
spawn = require('child_process').spawn,
dummy = spawn('python', ['/home/test/dummycommand.py']);

dummy.stdout.on('data', function (data) {
sys.print('stdout: ' + data);
});

>> when you run it, you have to wait some while before eventually getting :
stdout: dummy process 1 /10
dummy process 2 /10
dummy process 3 /10
dummy process 4 /10
dummy process 5 /10
dummy process 6 /10
dummy process 7 /10
dummy process 8 /10
dummy process 9 /10
dummy process 10 /10


I think I missed something ?

manimal45

unread,
Aug 23, 2010, 8:26:32 AM8/23/10
to nodejs

markus espenhain

unread,
Aug 23, 2010, 11:05:19 AM8/23/10
to nod...@googlegroups.com

probably ...

use sys.stdout.flush() after your print statement
http://docs.python.org/library/stdtypes.html
-> File Objects

or

$ python --help
...
-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
see man page for details on internal buffering relating to '-u'

markus

manimal45

unread,
Aug 24, 2010, 8:11:43 AM8/24/10
to nodejs
Dis I mention the dummyscript was written by dummycoder ?
Thanks for the help !
> use sys.stdout.flush() after your print statementhttp://docs.python.org/library/stdtypes.html

Graham Heath

unread,
Sep 8, 2016, 11:41:32 PM9/8/16
to nodejs
I just ran into this myself, but I was using a third party script and so chose not to edit the script.

I had to use Python's "unbuffered mode" using the PYTHONUNBUFFERED environment variable.

    var env = Object.create( process.env );
    env.PYTHONUNBUFFERED = '1';

    var ls = spawn('ls', this.args, {
      'env': env
    });

Thanks!
Reply all
Reply to author
Forward
0 new messages