Hi,
I’m definitely missing something and couldn’t find a complete example
execution of this code crashes when I import numpy but not for time
The problem only occurs in one thread (detached), the code called in the main thread works as expected
class PythonRunner
{
public:
//=============================================================================
void
runPythonCode(PyObject* globalDict, const wstringVector& commands)
{
std::string str = wstring_to_utf8(commands[0]) + "\n";
PyRun_SimpleString(str.c_str());
outputMessage += getPythonStandardOutput();
resultReady = true;
resultCondition.notify_one();
}
//=============================================================================
bool
isResultReady()
{
std::lock_guard<std::mutex> lock(resultMutex);
return resultReady;
}
//=============================================================================
std::wstring
getErrorMessage()
{
std::unique_lock<std::mutex> lock(resultMutex);
resultCondition.wait(lock, [this]() { return resultReady; });
return errorMessage;
}
//=============================================================================
std::wstring
getStandardOutput()
{
std::unique_lock<std::mutex> lock(resultMutex);
resultCondition.wait(lock, [this]() { return resultReady; });
return outputMessage;
}
//=============================================================================
private:
std::mutex resultMutex;
std::condition_variable resultCondition;
std::wstring errorMessage;
std::wstring outputMessage;
bool resultReady = false;
//=============================================================================
};
PythonRunner runner;
std::thread thread(
[&runner, &globalDict, &commands]() { runner.runPythonCode(globalDict, commands); });
thread.detach();
while (!runner.isResultReady()) {
// do others things
}
commands[0] = “import time; time.sleep(10)” OK it works
but commands[0] = “import numpy as np” not working in a detached thread but ok if called directly without thread
Help will be really appreciated