-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to use the PyGILState API in gil_scoped_acquire (#1276) #1286
base: master
Are you sure you want to change the base?
Conversation
9228f27
to
31e169c
Compare
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, |
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 |
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. |
What about just leaving it up to the caller to do the actual void my_class::some_func(int arg) {
return pybind11::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. |
We found another case for making a global configuration option: |
Here is a simple example that illustrates the problem with #include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <iostream>
class basic_gil_scoped_acquire
{
public:
basic_gil_scoped_acquire()
{
state = PyGILState_Ensure();
}
~basic_gil_scoped_acquire()
{
PyGILState_Release(state);
}
private:
PyGILState_STATE state;
};
class basic_gil_scoped_release
{
public:
basic_gil_scoped_release()
{
state = PyEval_SaveThread();
}
~basic_gil_scoped_release()
{
PyEval_RestoreThread(state);
}
private:
PyThreadState *state;
};
static const char pycode[] =
"raise Exception('This is a test')";
int main(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;
}
return 0;
} This will segfault when the |
@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 |
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.