Skip to content
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 14 commits into from
Mar 28, 2024

Conversation

taminob
Copy link
Owner

@taminob taminob commented Mar 27, 2024

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

taminob added 14 commits March 28, 2024 00:13
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.
@taminob taminob added bug Something isn't working python Related to python plugin labels Mar 27, 2024
@taminob taminob self-assigned this Mar 27, 2024
@taminob taminob enabled auto-merge (rebase) March 28, 2024 19:17
@taminob taminob disabled auto-merge March 28, 2024 19:18
@taminob taminob merged commit a985561 into main Mar 28, 2024
9 checks passed
@taminob taminob deleted the bugfix/python-plugin-multithreading branch March 28, 2024 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to python plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python plugin not thread-safe
1 participant