AndrewBenjamin
AndrewBenjamin

Reputation: 779

Cannot get Process to Execute before input stream is closed

The code is supposed to run python and accept python commands from the StreamWriter. But only closing the StreamWriter causes the code to execute - that's no good:

private Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
    FileName = "python",
    UseShellExecute = false,
    RedirectStandardInput = true,
    RedirectStandardOutput = false
};
p.Start();

//new Task(WriteInputTask).Start();

private StreamWriter sw = p.StandardInput;
sw.AutoFlush = true; //does nothing
sw.Write("print('Printing from python')" + Environment.NewLine);
sw.Flush(); //does nothing
sw.Close(); //NOW console shows "Printing from python"

I don't want to have to restart python and re-import everything (especially arcpy which takes half a minute to import) every time I want to issue new commands. Close() does something with the buffer that Flush() does not.

Upvotes: 3

Views: 257

Answers (1)

Dweeberly
Dweeberly

Reputation: 4777

Sorry took a little longer than I expected. This is a python oddity (i.e. you don't see this behavior in cmd). You need to add the '-i' switch to python when you start it. Here is a full working example.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace stackoverflow1 {
class Program {

    static void Main(string[] args) {
        var exe = "python";
        var arguments = "-i";

        Process p = new Process();
        p.StartInfo = new ProcessStartInfo() {
            FileName = exe,
            Arguments = arguments,
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            StandardOutputEncoding = Encoding.UTF8,
            StandardErrorEncoding = Encoding.UTF8,
            CreateNoWindow = false,
        };

        p.OutputDataReceived += new DataReceivedEventHandler(
            delegate (object sendingProcess, DataReceivedEventArgs outLine) {
                Console.WriteLine("{0}: {1}", exe, outLine.Data);
                });

        p.ErrorDataReceived += new DataReceivedEventHandler(
            delegate (object sendingProcess, DataReceivedEventArgs errLine) {
                Console.WriteLine("Error: " + errLine.Data);
                });

        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();

        StreamWriter sw = p.StandardInput;
        sw.AutoFlush = true; //does nothing

        if (exe == "cmd") {
            sw.WriteLine("echo hello");
            sw.WriteLine("echo 2+2");
            sw.WriteLine("echo Goodbye");
            }
        else { // assume python
            sw.WriteLine("print('Hello')");
            sw.WriteLine("2+2");
            sw.WriteLine("print('Printing from python')");
            sw.WriteLine("print('Goodbye')");
            }
        sw.Flush();

        System.Threading.Thread.Sleep(200);
        Console.WriteLine("Closing");
        sw.Close();

        Console.ReadKey();
        }
   }
}

Upvotes: 4

Related Questions