< 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
78%
Covered lines: 15
Uncovered lines: 4
Coverable lines: 19
Total lines: 57
Line coverage: 78.9%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
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(...)66.66%7671.42%
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>
 323    public PythonGlobalInterpreterManager(IOptions<PythonOptions> options, ILogger<PythonGlobalInterpreterManager> logge
 24    {
 325        _options = options;
 326        _logger = logger;
 327    }
 28
 29    /// <inheritdoc />
 30    public Task StartAsync(CancellationToken cancellationToken)
 31    {
 332        if (!_options.Value.AllowHostCodeExecution)
 033            return Task.CompletedTask;
 34
 335        if (_initialized)
 236            return Task.CompletedTask;
 37
 138        if (!string.IsNullOrEmpty(_options.Value.PythonDllPath))
 039            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", _options.Value.PythonDllPath);
 40
 41        try
 42        {
 143            PythonEngine.Initialize();
 044            _mainThreadState = PythonEngine.BeginAllowThreads();
 045        }
 146        catch (Exception e)
 47        {
 148            _logger.LogWarning(e, "Failed to initialize Python engine");
 149        }
 50
 151        _initialized = true;
 152        return Task.CompletedTask;
 53    }
 54
 55    /// <inheritdoc />
 656    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
 57}