2

I'm trying to write a C# program that captures the standard output in a python program. My problem is that all of the output comes after the program has executed rather than when it actually happens. As an example, for this python program:

print "Hello"
time.sleep(2)
print "Hello"

I would expect to get "Hello", a two second gap, and then another "Hello". The actual result is a two second gap and then "Hello", "Hello".

If I run the above python script from the command line, I get the desired behaviour. If the command prompt can do this, then I should be able to mimic that functionality without having to flush the buffer repeatedly.

I'm using this to run the process from C#:

_proc = new Process
            {
                StartInfo = new ProcessStartInfo
                                {
                                    FileName = "C:\\Python27\\python.exe",
                                    Arguments = pyScript,
                                    RedirectStandardError = true,
                                    UseShellExecute = false,
                                    RedirectStandardOutput = true,
                                    CreateNoWindow = true
                                }
            };
_proc.OutputDataReceived += ProcOnOutputDataReceived;
_proc.Start(); 
_proc.BeginOutputReadLine();

I can run this C# code (and changing the ProcessStartInfo properties above to run C# executable) and it behaves correctly:

Console.WriteLine("Hello");
Thread.Sleep(2000);
Console.WriteLine("Hello");

With this code I get "Hello", a two second gap, and then another "Hello".

Any idea why? How can I get the python interpreter to send the standard output as it happens?

johnB
  • 536
  • 1
  • 6
  • 14
  • Sounds like a buffering problem. Try flushing `sys.stdout` after the print. – Wooble Apr 22 '14 at 20:20
  • 2
    In Windows, the normal behavior is that standard output just piles up until the child process exits. But this may help: http://stackoverflow.com/a/10940793/424129 -- which points here -- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx -- but the guy whose answer it is deserves the credit! – 15ee8f99-57ff-4f92-890c-b56153 Apr 22 '14 at 20:20
  • I should have done more research. The question was answered here http://stackoverflow.com/questions/2380649/redirect-python-standard-input-output-to-c-sharp-forms-application?rq=1 – johnB Apr 23 '14 at 20:10
  • I wasn't even thinking about the interpreter option. I upvoted Chris Vig on the other thread. I would also like to point out that running in interpreter mode also means the calling program can send Python statements to its stdin... – Centijo Apr 25 '14 at 13:57

3 Answers3

7

I know this is old but running python with -u option (unbuffered) seems to be what you were after.

Unbuffered will not wait before flushing output. Running python without this option, output is flushed when:

  • The buffer is full
  • You call sys.stdout.flush()
  • The process ends
Benoit Dufresne
  • 318
  • 3
  • 9
2

You need to flush the output buffer.

import sys
sys.stdout.flush()

See this question: How to flush output of Python print?

Community
  • 1
  • 1
Centijo
  • 584
  • 6
  • 15
  • thanks but I need to be able to do this without updating each script to flush after a print. The command prompt can do this so I presume it is possible – johnB Apr 23 '14 at 06:12
0

Good (Capture standard output of a python program as it happens) Em console is NEED: import sys sys.stdout.flush()

GTIcontrol
  • 36
  • 2