Skip to content

Commit 8e3f362

Browse files
committed
first attempt to add python interpreter lock. Not working
1 parent db1ed53 commit 8e3f362

File tree

5 files changed

+37
-27
lines changed

5 files changed

+37
-27
lines changed

src/Bonsai.Scripting.Python/Bonsai.Scripting.Python.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Description>Bonsai Scripting Library for interfacing with the Python runtime.</Description>
66
<PackageTags>Bonsai Rx Scripting Python Python.NET</PackageTags>
77
<TargetFramework>net472</TargetFramework>
8-
<VersionPrefix>0.1.0</VersionPrefix>
8+
<VersionPrefix>0.2.0</VersionPrefix>
99
<LangVersion>8.0</LangVersion>
1010
</PropertyGroup>
1111

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Python.Runtime;
2+
using System;
3+
4+
public sealed class GilManager
5+
{
6+
private static readonly Lazy<GilManager> _instance = new Lazy<GilManager>(() => new GilManager());
7+
public static GilManager Instance => _instance.Value;
8+
9+
private GilManager()
10+
{
11+
}
12+
13+
public void ExecuteWithGil(Action action)
14+
{
15+
using (Py.GIL()) // Acquire GIL
16+
{
17+
action();
18+
}
19+
}
20+
}

src/Bonsai.Scripting.Python/PythonInterpreterLock.cs

+12-19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Bonsai.Scripting.Python
1212
public class PythonInterpreterLock : WorkflowExpressionBuilder
1313
{
1414
static readonly Range<int> argumentRange = Range.Create(lowerBound: 1, upperBound: 1);
15+
private static readonly object _lock = new object();
1516

1617
public PythonInterpreterLock()
1718
: this(new ExpressionBuilderGraph())
@@ -27,44 +28,36 @@ public PythonInterpreterLock(ExpressionBuilderGraph workflow)
2728

2829
public override Expression Build(IEnumerable<Expression> arguments)
2930
{
30-
// var source = arguments.Single();
31-
// var sourceType = source.Type.GetGenericArguments()[0]; // Get TSource from IObservable<TSource>
32-
// var factoryParameter = Expression.Parameter(typeof(IObservable<>).MakeGenericType(sourceType));
33-
Console.WriteLine($"here1 args: {arguments}");
3431
var source = arguments.FirstOrDefault();
3532
if (source == null)
3633
{
3734
throw new InvalidOperationException("There must be at least one input.");
3835
}
3936
var sourceType = source.Type.GetGenericArguments()[0]; // Get TSource from IObservable<TSource>
40-
Console.WriteLine($"here2 sourceType: {source.Type}");
41-
// var factoryParameter = Expression.Parameter(source.Type);
4237
var factoryParameter = Expression.Parameter(typeof(IObservable<>).MakeGenericType(sourceType), "factoryParam");
43-
Console.WriteLine($"here3 factoryParam: {factoryParameter}");
38+
4439
return BuildWorkflow(arguments, factoryParameter, selectorBody =>
4540
{
46-
Console.WriteLine($"here4 selectorBody: {selectorBody}");
4741
var selector = Expression.Lambda(selectorBody, factoryParameter);
48-
Console.WriteLine($"here5 selector: {selector}");
4942
var resultType = selectorBody.Type.GetGenericArguments()[0];
50-
Console.WriteLine($"here6 resultType: {resultType}");
51-
// var processMethod = typeof(PythonInterpreterLock).GetMethod(nameof(Process)).MakeGenericMethod(sourceType, resultType);
52-
// Console.WriteLine($"here7 processMethod: {processMethod}");
53-
// return Expression.Call(processMethod, source, selector);
5443
return Expression.Call(GetType(), nameof(Process), new Type[] {sourceType, resultType}, source, selector);
5544
});
5645
}
5746

5847
static IObservable<TResult> Process<TSource, TResult>(IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> selector)
5948
{
60-
Console.WriteLine("process");
61-
return Observable.Defer(() =>
49+
lock(_lock)
6250
{
63-
using (Py.GIL())
51+
return source.SelectMany(value =>
6452
{
65-
return selector(source);
66-
}
67-
});
53+
using (Py.GIL())
54+
{
55+
return selector(Observable.Return(value));
56+
// );
57+
}
58+
});
59+
}
60+
// return result;
6861
}
6962
}
7063
}

src/Bonsai.Scripting.Python/RuntimeManager.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ internal RuntimeManager(string pythonHome, string scriptPath, IObserver<RuntimeM
2828
{
2929
Initialize(pythonHome);
3030
threadState = PythonEngine.BeginAllowThreads();
31-
using (Py.GIL())
32-
{
33-
MainModule = CreateModule(scriptPath: scriptPath);
34-
}
31+
MainModule = CreateModule(scriptPath: scriptPath);
3532
observer.OnNext(this);
3633
});
3734
}

src/Bonsai.Scripting.Python/Set.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public override IObservable<TSource> Process<TSource>(IObservable<TSource> sourc
4949
{
5050
return source.Do(value =>
5151
{
52-
using (Py.GIL())
53-
{
52+
// using (Py.GIL())
53+
// {
5454
var module = Module ?? runtime.MainModule;
5555
module.Set(VariableName, value);
56-
}
56+
// }
5757
});
5858
});
5959
}

0 commit comments

Comments
 (0)