Untitled

 avatar
unknown
plain_text
2 years ago
957 B
3
Indexable
// Initialize a Python engine in each thread
for (int i = 0; i < numThreads; i++)
{
    // Initialize a new Python engine
    PythonEngine.Initialize();

    // Allow Python code to be executed concurrently in this thread
    PythonEngine.BeginAllowThreads();

    // Create a new thread to execute Python code
    Thread t = new Thread(() =>
    {
        // Execute the Python script
        PythonEngine.RunSimpleString(pythonScript);

        // Call a Python function from C#
        using (Py.GIL())
        {
            dynamic module = Py.Import("__main__");
            dynamic print_hello = module.print_hello;
            print_hello();
        }
    });

    // Start the thread
    t.Start();

    // End the thread's access to the Python engine
    PythonEngine.EndAllowThreads();

    // Wait for the thread to finish executing
    t.Join();

    // Cleanup the Python engine
    PythonEngine.Shutdown();
}
Editor is loading...