boost python with threads
It seems boost::python and boost::thread don't really like each other for what I can tell.
Please refer to http://pastebin.com/Cy123mJK
This is a simplification of the problems I am having with my boost::python and boost::thread-based application.
If anyone can tell me开发者_JS百科 why these problems are occurring; I have no clue, since I strictly make sure python interaction is done with one thread at once.
At some point, the program crashes with a segfault for no obvious reason. Also, it's impossible to catch this crash it seems...
Help much appreciated!
You are running python in multiple threads at the same time in Producer::run()
and Consumer::run()
.
To be exact, you run this before locking the mutex:
boost::python::object writer = this->k->Get<boost::python::object>("write");
Maybe you didn't realize that Boost eventually calls PyObject_GetItem
when you call boost::python::object::operator[](const std::string&)
in Keeper::Get
. You need to move that Get-call to the correct location, after locking and before using the returned function:
{
boost::mutex::scoped_lock l(this->k->python_keeper);
boost::python::object writer = this->k->Get<boost::python::object>("write");
writer(boost::python::str(os.str()));
}
Edit: Removed Py_Finalize()
. Yes you are right, boost.python doesn't like it.
精彩评论