diff --git a/StabilityMatrix/Helper/ProcessRunner.cs b/StabilityMatrix/Helper/ProcessRunner.cs index 104e9ac9..1c33bd84 100644 --- a/StabilityMatrix/Helper/ProcessRunner.cs +++ b/StabilityMatrix/Helper/ProcessRunner.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -28,7 +29,11 @@ public static class ProcessRunner return output; } - public static Process StartProcess(string fileName, string arguments, Action? outputDataReceived = null) + public static Process StartProcess( + string fileName, + string arguments, + Action? outputDataReceived = null, + Dictionary? environmentVariables = null) { Logger.Trace($"Starting process '{fileName}' with arguments '{arguments}'"); var process = new Process(); @@ -38,6 +43,14 @@ public static class ProcessRunner process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; + + if (environmentVariables != null) + { + foreach (var (key, value) in environmentVariables) + { + process.StartInfo.EnvironmentVariables[key] = value; + } + } if (outputDataReceived != null) { diff --git a/StabilityMatrix/PyVenvRunner.cs b/StabilityMatrix/PyVenvRunner.cs index e7c9b0cc..d3dad121 100644 --- a/StabilityMatrix/PyVenvRunner.cs +++ b/StabilityMatrix/PyVenvRunner.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; using StabilityMatrix.Helper; @@ -64,14 +65,25 @@ public class PyVenvRunner: IDisposable } } - public void RunDetached(string arguments, Action outputDataReceived, Action? onExit = null) + public void RunDetached(string arguments, Action outputDataReceived, Action? onExit = null, bool unbuffered = true) { if (!Exists()) { throw new InvalidOperationException("Venv python process does not exist"); } Debug.WriteLine($"Launching RunDetached at {PythonPath} with args {arguments}"); - Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived); + if (unbuffered) + { + var env = new Dictionary + { + {"PYTHONUNBUFFERED", "1"} + }; + Process = ProcessRunner.StartProcess(PythonPath, "-u " + arguments, outputDataReceived, env); + } + else + { + Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived); + } if (onExit != null) { Process.Exited += (_, _) => onExit(Process.ExitCode);