Class PythonNetManager

Singleton manager for the PythonNet runtime lifecycle. Must be initialized once at application startup before any prediction blocks attempt to acquire the GIL. Call Shutdown() on application exit to allow PythonEngine.Shutdown() to run cleanly. Thread-safe: GIL acquisition is the caller's responsibility per-call.

Inheritance
PythonNetManager
Implements
Namespace: MOSAIC.Components.Manager.Python
Assembly: MOSAIC.dll
Syntax
public sealed class PythonNetManager : IDisposable

Constructors

PythonNetManager(string)

Declaration
private PythonNetManager(string configPath)
Parameters
Type Name Description
string configPath

Fields

DeployedConfigFolder

Folder a Python config is deployed to beside the executable.

Declaration
public const string DeployedConfigFolder = "Python"
Field Value
Type Description
string
Remarks

A published build has no source tree to search, so the configs are copied here and this is the first place ResolveConfigPath(string) looks.

_disposed

Declaration
private bool _disposed
Field Value
Type Description
bool

_instance

Declaration
private static PythonNetManager? _instance
Field Value
Type Description
PythonNetManager

_lock

Declaration
private static readonly object _lock
Field Value
Type Description
object

_threadState

Declaration
private nint _threadState
Field Value
Type Description
nint

Properties

ConfigPath

Config file the live runtime was built from. Used to explain a second, conflicting request.

Declaration
public string ConfigPath { get; private set; }
Property Value
Type Description
string

Instance

Declaration
public static PythonNetManager Instance { get; }
Property Value
Type Description
PythonNetManager

PythonDllPath

Declaration
public string PythonDllPath { get; private set; }
Property Value
Type Description
string

ScriptsPath

Declaration
public string ScriptsPath { get; private set; }
Property Value
Type Description
string

SitePackagesPath

Declaration
public string SitePackagesPath { get; private set; }
Property Value
Type Description
string

Methods

AssertReady()

Declaration
public void AssertReady()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Declaration
public void Dispose()

Initialize(string)

Initializes the Python runtime from the given config file path. Safe to call only once; subsequent calls are no-ops if already initialized.

Declaration
public static PythonNetManager Initialize(string configPath)
Parameters
Type Name Description
string configPath
Returns
Type Description
PythonNetManager

Resolve(string, string?)

Call from each prediction block's constructor or Init() to confirm the runtime is live. Throws if manager was never initialized.

Declaration
private static string Resolve(string configDir, string? value)
Parameters
Type Name Description
string configDir

Folder holding the config file.

string value

Raw value from the config. May be absolute, relative, or empty.

Returns
Type Description
string

An absolute path.

ResolveConfigPath(string)

Locates a Python config file, looking beside the executable before the source tree.

Declaration
public static string ResolveConfigPath(string fileName)
Parameters
Type Name Description
string fileName

Config file name, e.g. config.json or configWulpus.json.

Returns
Type Description
string

Absolute path to the config file.

Remarks

Both orders matter. A published build only has the copy beside the executable, and looking there first also lets a developer test the deployed layout. The walk up to the solution root is the fallback that keeps dotnet run working from the source tree, where nothing has been copied to the output yet.

Exceptions
Type Condition
FileNotFoundException

Neither location holds the file.

RunWithGIL(Action<dynamic>)

Convenience wrapper: runs action under the GIL. Prefer this over raw Py.GIL() in block code to keep GIL usage central.

Declaration
public void RunWithGIL(Action<dynamic> action)
Parameters
Type Name Description
Action<dynamic> action

Shutdown()

Shuts down the Python runtime cleanly. Call once on application exit, e.g. from App.OnExit() or a hosted service StopAsync().

Declaration
public void Shutdown()

Implements

IDisposable

Search documentation

Start typing to search the documentation.