diff --git a/StabilityMatrix/PyRunner.cs b/StabilityMatrix/PyRunner.cs
index 4f091830..ebea5499 100644
--- a/StabilityMatrix/PyRunner.cs
+++ b/StabilityMatrix/PyRunner.cs
@@ -23,8 +23,8 @@ internal static class PyRunner
public static string PipExePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativePipExePath);
public static string GetPipPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeGetPipPath);
- public static PyIOStream StdOutStream;
- public static PyIOStream StdErrStream;
+ public static PyIOStream? StdOutStream;
+ public static PyIOStream? StdErrStream;
private static readonly SemaphoreSlim PyRunning = new(1, 1);
@@ -158,46 +158,24 @@ internal static class PyRunner
/// Evaluate Python expression and return its value
///
///
- public static async Task Eval(string expression)
+ public static Task Eval(string expression)
{
- await PyRunning.WaitAsync();
- try
- {
- return await Task.Run(() =>
- {
- using (Py.GIL())
- {
- var result = PythonEngine.Eval(expression);
- return result.As();
- }
- });
- }
- finally
+ return RunInThreadWithLock(() =>
{
- PyRunning.Release();
- }
+ var result = PythonEngine.Eval(expression);
+ return result.As();
+ });
}
///
/// Execute Python code without returning a value
///
///
- public static async Task Exec(string code)
+ public static Task Exec(string code)
{
- await PyRunning.WaitAsync();
- try
- {
- await Task.Run(() =>
- {
- using (Py.GIL())
- {
- PythonEngine.Exec(code);
- }
- });
- }
- finally
+ return RunInThreadWithLock(() =>
{
- PyRunning.Release();
- }
+ PythonEngine.Exec(code);
+ });
}
}
\ No newline at end of file