That won't be easy to do. You're suffering from buffering, but not in your program, but in the program you are calling. Typically, STDOUT is buffered, while STDERR isn't. Only when STDOUT is redirected to a terminal, it's flushed when a newline is emitted. But you don't call it from a terminal - you're capturing the output, so the output is buffered, and appears later than the unbuffered STDERR.
If you can tell the called program to run output unbuffered, than that's your easiest option.