N
N
nikitao2021-09-28 17:08:33
Python
nikitao, 2021-09-28 17:08:33

Why doesn't C# Process RedirectStandardInput work with python.exe?

Hello. Why is this example

ProcessStartInfo info=new ProcessStartInfo("python");
            info.RedirectStandardOutput = true;
            info.RedirectStandardInput = true;
            info.RedirectStandardError = true;
            info.UseShellExecute = false;
            //info.CreateNoWindow = true;
            Process process=new Process();
            process.OutputDataReceived += (s, e) =>
            {
                Console.WriteLine($"Output:{e.Data}");
            };
            process.ErrorDataReceived += (s, e) =>
            {
                Console.WriteLine($"Error:{e.Data}");
            };

            process.StartInfo = info;
            process.Start();
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();

            while (!process.HasExited)
            {
                Console.Write("Command:");
                var line= Console.ReadLine();
                process.StandardInput.WriteLine(line);
                process.StandardInput.Flush();
            }
            Console.WriteLine("Done");
            Console.ReadLine();


causes no data to come from the python process at all.
If you turn off RedirectStandardInput , then data comes in, but accordingly, there is no way to send commands from the parent thread, except by hand.
At the same time, if I do not start the python process, but for example just cmd , then everything works as it should.
Thanks in advance

PS
If IronPython doesn't fit, you need Python support from 3.7, and IronPython is now only 3.4 and then alpha

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question