Search code examples
pythonc++cpython

PyRun_SimpleString is blocking whenever import is used, never returns


std::cout << "im in here!\n";
PyGILState_STATE sMain = PyGILState_Ensure();

if (sMain)
{
    std::cout << "attempting\n";
    PyRun_SimpleString("import sys; sys.exit();");
    std::cout << "attempted!\n";
}

PyGILState_Release(sMain);

Seems attempted never prints however sMain is valid and exists PyRun_SimpleString never appears to return at all actually. Python code in question never appears to run either.

Edit: So i've narrowed the problem down to whenever I import a module in the string. Normal code works fine however if I import the function blocks and never returns


Solution

  • Fixed it, tried importing with PyImport_ImportModuleNoBlock and it reported that the module lock was held by another thread Python 2.7