< Summary

Information
Class: Elsa.Expressions.Python.HostedServices.PythonGlobalInterpreterManager
Assembly: Elsa.Expressions.Python
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Python/HostedServices/PythonGlobalInterpreterManager.cs
Line coverage
76%
Covered lines: 13
Uncovered lines: 4
Coverable lines: 17
Total lines: 54
Line coverage: 76.4%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
StartAsync(...)50%5466.66%
StopAsync(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Python/HostedServices/PythonGlobalInterpreterManager.cs

#LineLine coverage
 1using Elsa.Expressions.Python.Options;
 2using Microsoft.Extensions.Hosting;
 3using Microsoft.Extensions.Logging;
 4using Microsoft.Extensions.Options;
 5using Python.Runtime;
 6
 7namespace Elsa.Expressions.Python.HostedServices;
 8
 9/// <summary>
 10/// Initializes the Python engine.
 11/// </summary>
 12public class PythonGlobalInterpreterManager : IHostedService
 13{
 14    private readonly IOptions<PythonOptions> _options;
 15    private readonly ILogger _logger;
 16    private IntPtr _mainThreadState;
 17    private static bool _initialized;
 18
 19
 20    /// <summary>
 21    /// Initializes a new instance of the <see cref="PythonGlobalInterpreterManager"/> class.
 22    /// </summary>
 123    public PythonGlobalInterpreterManager(IOptions<PythonOptions> options, ILogger<PythonGlobalInterpreterManager> logge
 24    {
 125        _options = options;
 126        _logger = logger;
 127    }
 28
 29    /// <inheritdoc />
 30    public Task StartAsync(CancellationToken cancellationToken)
 31    {
 132        if (_initialized)
 033            return Task.CompletedTask;
 34
 135        if (!string.IsNullOrEmpty(_options.Value.PythonDllPath))
 036            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", _options.Value.PythonDllPath);
 37
 38        try
 39        {
 140            PythonEngine.Initialize();
 041            _mainThreadState = PythonEngine.BeginAllowThreads();
 042        }
 143        catch (Exception e)
 44        {
 145            _logger.LogWarning(e, "Failed to initialize Python engine");
 146        }
 47
 148        _initialized = true;
 149        return Task.CompletedTask;
 50    }
 51
 52    /// <inheritdoc />
 253    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
 54}