Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Executing Multiple GIL using Threads #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
YogesGovindaraj opened this issue Jan 7, 2025 · 0 comments
Closed

Executing Multiple GIL using Threads #2541

YogesGovindaraj opened this issue Jan 7, 2025 · 0 comments

Comments

@YogesGovindaraj
Copy link

YogesGovindaraj commented Jan 7, 2025

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.

@pythonnet pythonnet locked and limited conversation to collaborators Apr 11, 2025
@lostmsu lostmsu converted this issue into discussion #2579 Apr 11, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant