-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotPythonHost.cs
More file actions
173 lines (156 loc) · 5.69 KB
/
Copy pathDotPythonHost.cs
File metadata and controls
173 lines (156 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Diagnostics.CodeAnalysis;
using DotPython.Contracts;
using DotPython.Runtime.Managed;
namespace DotPython.Hosting;
/// <summary>
/// Owns a managed DotPython runtime and typed module clients for small applications.
/// </summary>
public sealed class DotPythonHost : IAsyncDisposable
{
private readonly object _gate = new();
private readonly DotPythonModuleProvider _provider;
private readonly IDotPythonModuleRuntime _runtime;
private readonly List<DotPythonModuleSession> _sessions = [];
private Task? _disposeTask;
private bool _disposed;
internal DotPythonHost(IDotPythonModuleRuntime runtime)
{
_runtime = runtime;
_provider = new DotPythonModuleProvider(runtime);
}
/// <summary>Creates a host that owns a managed DotPython runtime.</summary>
[SuppressMessage(
"Reliability",
"CA2000:Dispose objects before losing scope",
Justification = "The returned host takes ownership of the managed runtime."
)]
public static DotPythonHost CreateManaged() => new(new ManagedPythonModuleRuntime());
/// <summary>
/// Creates a host that takes ownership of a backend-independent module runtime.
/// </summary>
public static DotPythonHost Create(IDotPythonModuleRuntime runtime)
{
ArgumentNullException.ThrowIfNull(runtime);
return new DotPythonHost(runtime);
}
/// <summary>Creates a typed client for a per-runtime module.</summary>
public TService GetModule<TService>(PythonModuleRegistration<TService> registration)
where TService : class => GetModule(registration, static _ => { });
/// <summary>Creates a configured typed client for a per-runtime module.</summary>
public TService GetModule<TService>(
PythonModuleRegistration<TService> registration,
Action<DotPythonModuleHostingOptions> configure
)
where TService : class
{
ArgumentNullException.ThrowIfNull(registration);
ArgumentNullException.ThrowIfNull(configure);
if (registration.StatePolicy != PythonModuleStatePolicy.PerRuntime)
{
throw new InvalidOperationException(
"Per-session modules must be resolved from a DotPythonModuleSession."
);
}
lock (_gate)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ConfigureProvider(_provider, registration.Definition, configure);
return registration.CreateClient(_provider);
}
}
/// <summary>Loads and validates a per-runtime module before its first invocation.</summary>
public ValueTask WarmUpAsync<TService>(
PythonModuleRegistration<TService> registration,
CancellationToken cancellationToken = default
)
where TService : class => WarmUpAsync(registration, static _ => { }, cancellationToken);
/// <summary>Loads and validates a configured per-runtime module.</summary>
public ValueTask WarmUpAsync<TService>(
PythonModuleRegistration<TService> registration,
Action<DotPythonModuleHostingOptions> configure,
CancellationToken cancellationToken = default
)
where TService : class
{
ArgumentNullException.ThrowIfNull(registration);
ArgumentNullException.ThrowIfNull(configure);
if (registration.StatePolicy != PythonModuleStatePolicy.PerRuntime)
{
throw new InvalidOperationException(
"Per-session modules must be warmed within a DotPythonModuleSession."
);
}
lock (_gate)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ConfigureProvider(_provider, registration.Definition, configure);
return _provider.WarmUpAsync(registration.Definition, cancellationToken);
}
}
/// <summary>
/// Creates a logical state scope for per-session modules. This is not a security boundary.
/// </summary>
public DotPythonModuleSession CreateSession()
{
lock (_gate)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var session = new DotPythonModuleSession(_runtime, RemoveSession);
_sessions.Add(session);
return session;
}
}
/// <inheritdoc />
public ValueTask DisposeAsync()
{
lock (_gate)
{
if (_disposeTask is not null)
{
return new ValueTask(_disposeTask);
}
_disposed = true;
_disposeTask = DisposeCoreAsync(_sessions.AsEnumerable().Reverse().ToArray());
return new ValueTask(_disposeTask);
}
}
private async Task DisposeCoreAsync(IReadOnlyList<DotPythonModuleSession> sessions)
{
try
{
foreach (var session in sessions)
{
await session.DisposeFromHostAsync().ConfigureAwait(false);
}
}
finally
{
try
{
await _provider.DisposeAsync().ConfigureAwait(false);
}
finally
{
await _runtime.DisposeAsync().ConfigureAwait(false);
}
}
}
private void RemoveSession(DotPythonModuleSession session)
{
lock (_gate)
{
_sessions.Remove(session);
}
}
internal static void ConfigureProvider(
DotPythonModuleProvider provider,
PythonModuleDefinition definition,
Action<DotPythonModuleHostingOptions> configure
)
{
var options = new DotPythonModuleHostingOptions();
configure(options);
options.Validate();
provider.ConfigureInitialization(definition, options.MaximumInitializationAttempts);
}
}