@@ -12,6 +12,7 @@ namespace Bonsai.Scripting.Python
12
12
public class PythonInterpreterLock : WorkflowExpressionBuilder
13
13
{
14
14
static readonly Range < int > argumentRange = Range . Create ( lowerBound : 1 , upperBound : 1 ) ;
15
+ private static readonly object _lock = new object ( ) ;
15
16
16
17
public PythonInterpreterLock ( )
17
18
: this ( new ExpressionBuilderGraph ( ) )
@@ -27,44 +28,36 @@ public PythonInterpreterLock(ExpressionBuilderGraph workflow)
27
28
28
29
public override Expression Build ( IEnumerable < Expression > arguments )
29
30
{
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 } ") ;
34
31
var source = arguments . FirstOrDefault ( ) ;
35
32
if ( source == null )
36
33
{
37
34
throw new InvalidOperationException ( "There must be at least one input." ) ;
38
35
}
39
36
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);
42
37
var factoryParameter = Expression . Parameter ( typeof ( IObservable < > ) . MakeGenericType ( sourceType ) , "factoryParam" ) ;
43
- Console . WriteLine ( $ "here3 factoryParam: { factoryParameter } " ) ;
38
+
44
39
return BuildWorkflow ( arguments , factoryParameter , selectorBody =>
45
40
{
46
- Console . WriteLine ( $ "here4 selectorBody: { selectorBody } ") ;
47
41
var selector = Expression . Lambda ( selectorBody , factoryParameter ) ;
48
- Console . WriteLine ( $ "here5 selector: { selector } ") ;
49
42
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);
54
43
return Expression . Call ( GetType ( ) , nameof ( Process ) , new Type [ ] { sourceType , resultType } , source , selector ) ;
55
44
} ) ;
56
45
}
57
46
58
47
static IObservable < TResult > Process < TSource , TResult > ( IObservable < TSource > source , Func < IObservable < TSource > , IObservable < TResult > > selector )
59
48
{
60
- Console . WriteLine ( "process" ) ;
61
- return Observable . Defer ( ( ) =>
49
+ lock ( _lock )
62
50
{
63
- using ( Py . GIL ( ) )
51
+ return source . SelectMany ( value =>
64
52
{
65
- return selector ( source ) ;
66
- }
67
- } ) ;
53
+ using ( Py . GIL ( ) )
54
+ {
55
+ return selector ( Observable . Return ( value ) ) ;
56
+ // );
57
+ }
58
+ } ) ;
59
+ }
60
+ // return result;
68
61
}
69
62
}
70
63
}
0 commit comments