2

In pybind11-wrapped c++, called from python, I can do this:

py::array_t<double> t1 = py::array_t<double>(3);

But: if I do that in a separate thread, it crashes with a segmentation fault (actually it seems to crash when t1 goes out of scope or gets destructed).

I can fix this by doing

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

pybind11::array_t<double> t1 = pybind11::array_t<double>(3)

PyGILState_Release(gstate);

So obviously there is some GIL-dependent stuff in pybind11::array_t. Is it necessarily so? Do I have to acquire the GIL in order to instantiate it?

Colin
  • 3,670
  • 1
  • 25
  • 36

1 Answers1

2

Yes, GIL is required.

When you create a python object you are interacting with python interpreter. At some point of the process you need to acquire GIL to do it right.

That said, creation of pybind11::array_t might have expensive part which doesn't require GIL and could be done in another thread. For example you may allocate and initialize raw data and pass it to pybind11::array_t constructor to quickly create object with GIL.

Sergei
  • 1,599
  • 12
  • 21