Streaming stdout from a neko.io.Process

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Streaming stdout from a neko.io.Process

David Peek
Hi All,

Let me preface this by saying that I still feel new to the world of system programming, so please forgive me if this is noobish. It's HaXe's fault for releasing us UI programmers on the system. Run for the hills!

I'm wondering how best to stream the output from a process back to the current process. Let's say I were to launch haxe to compile a cpp application. When I do this:

var process = new neko.io.Process(command, args);

if (process.exitCode() == 0)
{
        Lib.println(process.stdout.readAll().toString());
}

The program waits until the process has finished and then prints the output, which makes sense because Process.exitCode() is blocking, as per the documentation. Of course it also looks like my program has hung, because compiling a cpp target takes a while and I'm not printing stdout as it is produced, only when the process exits. I've tried a few other things like a while loop that exits on EOF

var process = new neko.io.Process(command, args);

while (true)
{
        try
        {
                Lib.println(process.stdout.readLine());
        }
        catch (e:haxe.io.Eof)
        {
                break;
        }
}

if (process.exitCode() > 0)
{
        Lib.println(process.stderr.readAll().toString());
}

And various other combinations. Just wondering if there's a standard way of "forwarding" stdout/stderr from a child process to the current process.

Sorry for the long email, and thanks :)
David


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Streaming stdout from a neko.io.Process

Ian Martins


On Wed, Jun 15, 2011 at 1:21 AM, David Peek <[hidden email]> wrote:
Hi All,

Let me preface this by saying that I still feel new to the world of system programming, so please forgive me if this is noobish. It's HaXe's fault for releasing us UI programmers on the system. Run for the hills!

I'm wondering how best to stream the output from a process back to the current process. Let's say I were to launch haxe to compile a cpp application. When I do this:

var process = new neko.io.Process(command, args);

if (process.exitCode() == 0)
{
       Lib.println(process.stdout.readAll().toString());
}

The program waits until the process has finished and then prints the output, which makes sense because Process.exitCode() is blocking, as per the documentation. Of course it also looks like my program has hung, because compiling a cpp target takes a while and I'm not printing stdout as it is produced, only when the process exits. I've tried a few other things like a while loop that exits on EOF

var process = new neko.io.Process(command, args);

while (true)
{
       try
       {
               Lib.println(process.stdout.readLine());
       }
       catch (e:haxe.io.Eof)
       {
               break;
       }
}

if (process.exitCode() > 0)
{
       Lib.println(process.stderr.readAll().toString());
}

And various other combinations. Just wondering if there's a standard way of "forwarding" stdout/stderr from a child process to the current process.

Sorry for the long email, and thanks :)
David


I don't think there is a way of "forwarding" streams.  your solution seems fine.  I would put the "try" on the outside of the "while."

--
haXe - an open source web programming language
http://haxe.org