How do I redirect stderr of a spawned process into an internal buffer?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Nov 27 14:37:24 PST 2011


I've talked about this before, there's a problem with spawning
multiple processes and letting them write to stderr asynchronously It
seems like this might be a Windows-only problem, I couldn't recreate
on Ubuntu but maybe that's because it was virtualized.

Take buggy.d:

import std.concurrency;
import std.process;

void test(int i)
{
    system("dmd buggy.d buggy.d");
}

void main()
{
    foreach (i; 0 .. 100)
    {
        spawn(&test, i);
    }
}

Run it via rdmd: rdmd buggy.d

The output is messy (ran via cmd.exe): http://paste.pocoo.org/show/513763/

So it just gets worse the more processes are spawned. One workaround
is to redirect each process' stderr to a unique file. Example:

import core.thread;
import std.concurrency;
import std.process;
import std.string;
import std.stdio;
import std.file : readText;

void test(int i)
{
    // redirect each process stderr to its own file
    system(format("dmd workaround.d workaround.d 2> error_%s.txt", i));
}

void main()
{
    foreach (i; 0 .. 100)
    {
        spawn(&test, i);
    }

    thread_joinAll();

    foreach (i; 0 .. 100)
    {
        writeln(format("Output %s = %s", i,
readText(format("error_%s.txt", i))));
    }
}

So now the output is perfect: http://paste.pocoo.org/show/513771/

But writing to files is rather slow. Is there anything in Phobos I can
use to spawn a process and redirect stderr to an internal buffer?

Tango had this sort of thing for D1, but I'm not seeing anything in
Phobos. Since I can only recreate this on Windows I'm ok with using a
WinAPI function if it provides this feature.


More information about the Digitalmars-d-learn mailing list