-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[TPython] Make TPython thread-safe #16427
Conversation
2dcb3f7
to
24d6c3e
Compare
Test Results 13 files 13 suites 3d 4h 25m 34s ⏱️ Results for commit b371183. ♻️ This comment has been updated with latest results. |
3c172a2
to
a9ad23a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
e0d6bce
to
a0a8c9d
Compare
I have successfully made the following ROOT macro work: void task1(int i)
{
std::stringstream code;
code << "print(" << i << ")";
TPython::Exec(code.str().c_str());
}
void repro()
{
int nThreads = 1000;
std::vector<std::thread> threads;
for (int i = 0; i < nThreads; i++) {
threads.emplace_back(task1, i);
}
for (int i = 0; i < threads.size(); i++) {
threads[i].join();
}
} I'll turn this into a unit test tomorrow. |
a0a8c9d
to
c87d0a8
Compare
c87d0a8
to
23e9fd8
Compare
No static `gPythonizations` variable to avoid static initialization order fiasco. This avoids some crashes when using `TPython` in compiled code. This is necessary to make a newly suggested TPython multithreading unit test work: root-project/root#16427
b7041b6
to
aa5ada6
Compare
Analogous to commit 2b64c0f, where the same was done in the CPyCppyy API.
No static `gPythonizations` variable to avoid static initialization order fiasco. This avoids some crashes when using `TPython` in compiled code.
The PyGILState_Ensure() and PyGILState_Release() functions should be used as part of idiomatic code that calls Python from a C/C++ thread: https://docs.python.org/3/c-api/init.html#non-python-created-threads Also prevent concurrently initializing the Python interpreter by putting a lock in the private `TPython::Initialise()` function. This addresses the following issue reported by the Gaudi framework developers: https://gitlab.cern.ch/gaudi/Gaudi/-/issues/329#note_8433064
One would need to acquire the GIL at destruction time to make this work in multithreading setups.
aa5ada6
to
b371183
Compare
The PyGILState_Ensure() and PyGILState_Release() functions should be used as part of idiomatic code that calls Python from a C/C++ thread:
https://docs.python.org/3/c-api/init.html#non-python-created-threads
This addresses the following issue reported by the Gaudi framework developers:
https://gitlab.cern.ch/gaudi/Gaudi/-/issues/329#note_8433064