Uh oh!
There was an error while loading. Please reload this page.
Add option to use the PyGILState API in gil_scoped_acquire (#1276) - #1286
Add option to use the PyGILState API in gil_scoped_acquire (#1276)#1286KyleFromKitware wants to merge 1 commit into
Conversation
9228f27 to
31e169cComparejagerman
commented
Feb 17, 2018
This patch seems rather complicated (going via the global option). It also has the problem that the global setting could get you into trouble: for example, I think a simpler approach would be to entirely isolate the basic Python API approach into its own RAII class. Call them, perhaps, |
KyleFromKitware
commented
Feb 19, 2018
One of the things I found during testing of this patch is that each shared library or executable gets its own copy of the global |
jagerman
commented
Feb 19, 2018
Oh, right, I forgot about the macros. The number of different macros there is already getting unwieldy; doubling them up doesn't sound nice at all. (I'd like to somehow replace all those macros, but I don't yet have an idea of how to go about eliminating them--mainly because, at least right now, the macro needs to be able to That said, there's another advantage of having a different class: it ought to be possible to detect a deadlock when mixing implementations (like the one you ran into originally), at least in some cases, and raise an exception instead. |
KyleFromKitware
commented
Feb 19, 2018
What about just leaving it up to the caller to do the actual voidmy_class::some_func(int arg) {
returnpybind11::overload(this, "some_func", arg);
}That's just an example, it's nowhere near complete, and it probably wouldn't actually look like that. It seems to me the issue is more with templated functions being more unwieldy than macros. |
KyleFromKitware
commented
Mar 2, 2018
We found another case for making a global configuration option: |
KyleFromKitware
commented
Mar 2, 2018
Here is a simple example that illustrates the problem with #include<pybind11/pybind11.h>
#include<pybind11/embed.h>
#include<iostream>classbasic_gil_scoped_acquire
{
public:basic_gil_scoped_acquire()
{
state = PyGILState_Ensure();
}
~basic_gil_scoped_acquire()
{
PyGILState_Release(state);
}
private:
PyGILState_STATE state;
};
classbasic_gil_scoped_release
{
public:basic_gil_scoped_release()
{
state = PyEval_SaveThread();
}
~basic_gil_scoped_release()
{
PyEval_RestoreThread(state);
}
private:
PyThreadState *state;
};
staticconstchar pycode[] =
"raise Exception('This is a test')";
intmain(int argc, char **argv)
{
pybind11::scoped_interpreter interp;
basic_gil_scoped_release release;
try
{
basic_gil_scoped_acquire acquire;
pybind11::exec(pycode);
}
catch (pybind11::error_already_set &e)
{
std::cout << e.what() << std::endl;
}
return0;
}This will segfault when the |
scdub
commented
Feb 4, 2022
@KyleFromKitware is there anything that I can do to help move this PR forward? In embedded Python deployments this continues to prevent working with libraries that use the |
rwgk
commented
Feb 4, 2022
Just to log: I looked, and I have the same worries as @jagerman (Feb 17, 2018). Threading is a can of worms in general. Mixing in the pitfalls of Another thought, which may be naive: could the proposed It doesn't solve the issue with the OVERRIDE macros. |
No description provided.