Closed Kiryuumaru closed 7 months ago
I don't know how print
works in Python, but do you need to flush the data by any chance?
In python theres no need to flush when printing. This code will run normally when I execute it directly with python test.py "Joe"
. This will print every seconds Hi, Joe
x10 .
Does print
produce a newline at the end? ListenAsync()
looks for line breaks.
Yes. I also tried to manually add \n
to the print
Not sure then
Could be related to #214
Were you able to work around this issue? Did you try using the raw Process
class and see if it behaves differently with OutputDataReceived
?
Can you try to add -u
attribute:
await foreach (var cmdEvent in Cli.Wrap("python").WithArguments("-u test.py Joe").ListenAsync())
{
switch (cmdEvent)
{
case StartedCommandEvent started:
Console.WriteLine($"Process started; ID: {started.ProcessId}");
break;
case StandardOutputCommandEvent stdOut:
Console.WriteLine($"Out> {stdOut.Text}");
break;
case StandardErrorCommandEvent stdErr:
Console.WriteLine($"Err> {stdErr.Text}");
break;
case ExitedCommandEvent exited:
Console.WriteLine($"Process exited; Code: {exited.ExitCode}");
break;
}
}
Or set environment before: PYTHONUNBUFFERED=x
More info: -u
forces the stdout and stderr streams to be unbuffered; this option has no effect on stdin; also PYTHONUNBUFFERED=x
Version
3.6.4
Details
Using linux. I have a python code that prints a string every seconds for 10 sconds. I want to read the output every seconds using this code:
The python code
test.py
But this only prints everything after 10 seconds. Not every seconds. What might be the problem with this snippet.
Steps to reproduce
Use linux with python