Browse Source

Merge pull request #19 from ionite34/venvrunner-output-improve

Fix console output delays by setting python's unbuffered stdout flags
pull/5/head
JT 2 years ago committed by GitHub
parent
commit
7bf904afbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      StabilityMatrix/Helper/ProcessRunner.cs
  2. 16
      StabilityMatrix/PyVenvRunner.cs

15
StabilityMatrix/Helper/ProcessRunner.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -28,7 +29,11 @@ public static class ProcessRunner
return output; return output;
} }
public static Process StartProcess(string fileName, string arguments, Action<string?>? outputDataReceived = null) public static Process StartProcess(
string fileName,
string arguments,
Action<string?>? outputDataReceived = null,
Dictionary<string, string>? environmentVariables = null)
{ {
Logger.Trace($"Starting process '{fileName}' with arguments '{arguments}'"); Logger.Trace($"Starting process '{fileName}' with arguments '{arguments}'");
var process = new Process(); var process = new Process();
@ -39,6 +44,14 @@ public static class ProcessRunner
process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true; process.StartInfo.CreateNoWindow = true;
if (environmentVariables != null)
{
foreach (var (key, value) in environmentVariables)
{
process.StartInfo.EnvironmentVariables[key] = value;
}
}
if (outputDataReceived != null) if (outputDataReceived != null)
{ {
process.OutputDataReceived += (_, args) => outputDataReceived(args.Data); process.OutputDataReceived += (_, args) => outputDataReceived(args.Data);

16
StabilityMatrix/PyVenvRunner.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using StabilityMatrix.Helper; using StabilityMatrix.Helper;
@ -64,14 +65,25 @@ public class PyVenvRunner: IDisposable
} }
} }
public void RunDetached(string arguments, Action<string?> outputDataReceived, Action<int>? onExit = null) public void RunDetached(string arguments, Action<string?> outputDataReceived, Action<int>? onExit = null, bool unbuffered = true)
{ {
if (!Exists()) if (!Exists())
{ {
throw new InvalidOperationException("Venv python process does not exist"); throw new InvalidOperationException("Venv python process does not exist");
} }
Debug.WriteLine($"Launching RunDetached at {PythonPath} with args {arguments}"); Debug.WriteLine($"Launching RunDetached at {PythonPath} with args {arguments}");
Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived); if (unbuffered)
{
var env = new Dictionary<string, string>
{
{"PYTHONUNBUFFERED", "1"}
};
Process = ProcessRunner.StartProcess(PythonPath, "-u " + arguments, outputDataReceived, env);
}
else
{
Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived);
}
if (onExit != null) if (onExit != null)
{ {
Process.Exited += (_, _) => onExit(Process.ExitCode); Process.Exited += (_, _) => onExit(Process.ExitCode);

Loading…
Cancel
Save