Visitar URL original
Executing Multiple GIL using Threads · Issue #2541 · pythonnet/pythonnet · GitHub
Skip to content

Executing Multiple GIL using Threads #2541

Description

@YogesGovindaraj

Environment

  • 3.0.5
  • 3.9:
  • Windows:
  • 6.0:

Details

  • As i'm working on POC using C# Console Application .I have a requirement on executing multiple "PY.GIL" to handle different tasks using threads . The sample code is able to reach "PY.GIL" initialization after thread call but after that there is no progress the screen got freeze and shows only black screen.

  • The below is the sample code used:
    using System;
    using System.Threading;
    using Python.Runtime;
    Class program
    {
    public static void Main(string[] args)
    {
    var virtualpath="Specify the Path";
    Runtime.PythonDLL="Specify the DLL path";
    .///....Set Environment Variables...
    PythonEngine.PythonPath="pythonpath"+"Environment Variables path";
    PythonEngine.PythonHome=virtualpath;
    PythonEngine.Initialize();
    // Creating Threads..
    Thread thread1=new Thread(() => RunCode("Executing From Thread1");
    Thread thread2=new Thread(() => RunCode("Executing From Thread2");

thread1.Start();
thread2.Start();

thread1.join();
thread2.join();
//Shutdown PythonEngine
PythonEnginer.ShutDown();
}
static void RunCode(string threadName)
{
try
{
using(Py.GIL())
{
dynamic math=Py.Import("math");
double sqrtResult=math.sqrt(36);
double powResult=math.pow(2,3);
//print results
Console.WriteLine($"{threadName}: sqrt(25)={sqrtResult}, 2^3={powResult});
Console.ReadLine();
}

}
catch(Exception ex)
{
Console.WriteLine(ex.string());
}
}
}

Additional Details:

  1. I'm not getting any errors while reaching "Py.GIL", getting blank screen.

Questions:

How can i execute multiple GIL in parallel to handle python local invocation for different use cases..
I am grateful for any suggestions or insights into resolving this issue.

Activity

  1. locked and limited conversation to collaborators on Apr 11, 2025
  2. converted this issue into a discussion #2579 on Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions