-
Notifications
You must be signed in to change notification settings - Fork 0
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
Python plugin multithreading support #7
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The classes PythonInterpreter, PythonGuard, PythonObject and PythonTuple were added to wrap Python's C API into C++ friendly interfaces.
Replace PyGILState* API by PyEval_{Acquire,Release}Thread API. This is necessary since only the latter supports sub-interpreters in Python 3.12.
This configuration is especially designed to be used when embedding the python interpreter in another application.
Use PyErr_GetRaisedException if available and wrap in PythonObjects for correct PyObject* reference cleanup. Additionally, add bool cast to PythonException and PyObject.
For this, PythonException::occurred() is introduced to check if the error indicator is set in Python. PythonObject::to<std::string>() is added to allow formatting of the exception. PythonInterpreter::call() was modified to handle exceptions raised during execution of the called function.
Since some of the required functionality is not supported, it was supersided by own python C API wrappers. This is to avoid weird bugs due to GIL and Python's internal global states and the Boost.Python library and custom functions interfering with each other.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces multi-threading support for different instances of
PythonPlugin
.Previously, due to the global state used internally in the CPython library, two different plugins could not be used thread-safely.
This is achieved by using the Global Interpreter Lock (GIL) and (for Python 3.12 or newer) different sub-interpreters.
However, sub-interpreters are poorly documented and thus the implementation might not be perfect and still have some thread-unsafe calls.
Resolves #6