From 46e8588a186c01b0e5ed2ad56633bf611ac2b591 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 26 May 2023 01:02:43 -0400 Subject: [PATCH 1/2] Add optional env var config for StartProcess --- StabilityMatrix/Helper/ProcessRunner.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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) { From 582980bf6418d31603567456dd68cf651f6dc3b0 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 26 May 2023 01:03:03 -0400 Subject: [PATCH 2/2] Default RunDetached to set unbuffered python flags --- StabilityMatrix/PyVenvRunner.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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);