Jupyter Kernel Notebook Dies After Starting Threaded Class
- Dominant language
- HTML
- Stars
- 8.6k
- Forks
- 1.9k
- Avg merge
- 39m
- Merged PRs (30d)
- 2
Description
I am using Boost Python to expose a function that holds an internal thread, the class has ```start``` and ```stop``` functions that only control an internal loop. The code for ```MyClass``` is:
```
#include
#include
#include
#include
#include
#include
#include
class MyClass
{
private:
std::atomic running_;
std::thread worker_;
std::function m_op;
void printMessage()
{
std::string s = "hello";
while (running_)
{
s = m_op(s);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
public:
MyClass() : running_(false) {}
MyClass(const std::function& op) : running_(false), m_op(op) {}
void start()
{
if (!running_)
{
running_ = true;
worker_ = std::thread(&MyClass::printMessage, this);
}
}
void stop()
{
if (running_)
{
running_ = false;
worker_.join();
}
}
};
```
I then create a helper function to pass a callback as:
```
template
struct function_helper
{
function_helper(const boost::python::object& op) : m_op(op) {}
output_tp operator()(input_tps&&... args)
{
return boost::python::call(m_op.ptr(), std::forward(args)...);
}
private:
boost::python::object m_op;
};
boost::shared_ptr makeMakeClass(const boost::python::object& obj)
{
return boost::make_shared(function_helper(obj));
}
```
I then create my module:
```
BOOST_PYTHON_MODULE(draft)
{
using namespace boost::python;
class_, boost::shared_ptr, boost::noncopyable>("MyClass")
.def("__init__", make_constructor(makeMakeClass))
.def("start", &MyClass::start)
.def("stop", &MyClass::stop);
}
```
I use the ```draft``` module in jupyter as follows attached
The first run causes a crash as in

The second run is not throwing but produces nothing. Could you please help me make this work.
**UPDATE**
I figured a partial source of the problem, this line ```boost::python::call``` is what is causing issues, wondering if it's a memory thing when copying ```const boost::python::object& op```. Still stuck on how to make to the above work though.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.