-
Notifications
You must be signed in to change notification settings - Fork 222
Add test for multi-threaded import of multi-phase module #524
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
Draft
o01eg
wants to merge
1
commit into
boostorg:develop
Choose a base branch
from
o01eg:multi_thread_import
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // Copyright Stefan Seefeld 2007. | ||
| // Distributed under the Boost Software License, Version 1.0. (See | ||
| // accompanying file LICENSE_1_0.txt or copy at | ||
| // http://www.boost.org/LICENSE_1_0.txt) | ||
|
|
||
| #include <boost/python.hpp> | ||
|
|
||
| #include <boost/detail/lightweight_test.hpp> | ||
| #include <boost/bind/bind.hpp> | ||
| #include <iostream> | ||
| #include <thread> | ||
|
|
||
| namespace bpl = boost::python; | ||
|
|
||
| struct TestState | ||
| { | ||
| int x; | ||
| }; | ||
|
|
||
| int get_state() | ||
| { | ||
| TestState* state = reinterpret_cast<TestState*>(PyModule_GetState(bpl::import("multi_thread_import_").ptr())); | ||
| if (state) | ||
| { | ||
| return state->x; | ||
| } | ||
| return -2; | ||
| } | ||
|
|
||
| bool set_state(int value) | ||
| { | ||
| TestState* state = reinterpret_cast<TestState*>(PyModule_GetState(bpl::import("multi_thread_import_").ptr())); | ||
| if (state) | ||
| { | ||
| state->x = value; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| BOOST_PYTHON_MODULE_WITH_STATE(multi_thread_import_, TestState) { | ||
| state->x = -1; | ||
| bpl::def("get_state", get_state); | ||
| bpl::def("set_state", set_state); | ||
| } | ||
|
|
||
| void import_test(int thread_id) | ||
| { | ||
| // Retrieve the main module | ||
| bpl::object import_ = bpl::import("multi_thread_import_"); | ||
| bool result = bpl::extract<bool>(import_.attr("set_state")(thread_id)) BOOST_EXTRACT_WORKAROUND; | ||
| BOOST_TEST(result); | ||
|
|
||
| PyThreadState* _save = PyEval_SaveThread(); | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
| PyEval_RestoreThread(_save); | ||
|
|
||
| import_ = bpl::import("multi_thread_import_"); | ||
| int value = bpl::extract<int>(import_.attr("get_state")()) BOOST_EXTRACT_WORKAROUND; | ||
| std::cout << "Thread " << thread_id << ": " << value << std::endl; | ||
| BOOST_TEST(value == thread_id); | ||
| } | ||
|
|
||
| void thread_func(int thread_id, PyInterpreterState* main_interp) | ||
| { | ||
| // Initialize thread state for main interpreter in sub thread | ||
| PyThreadState* main_tstate = PyThreadState_New(main_interp); | ||
|
|
||
| if (!main_tstate) { | ||
| BOOST_ERROR("Failed to create main thread state"); | ||
| return; | ||
| } | ||
|
|
||
| // Lock GIL before create sub interpreter | ||
| PyEval_AcquireThread(main_tstate); | ||
|
|
||
| // Create sub interpreter | ||
| PyThreadState* sub_thread_state = Py_NewInterpreter(); | ||
|
|
||
| if (!sub_thread_state) { | ||
| BOOST_ERROR("Failed to create sub-interpreter"); | ||
| // Release GIL | ||
| PyEval_ReleaseThread(main_tstate); | ||
| // Delete sub thread state | ||
| PyThreadState_Delete(main_tstate); | ||
| return; | ||
| } | ||
|
|
||
| if (bpl::handle_exception(boost::bind(import_test, thread_id))) | ||
| { | ||
| if (PyErr_Occurred()) | ||
| { | ||
| BOOST_ERROR("Python Error detected"); | ||
| PyErr_Print(); | ||
| } | ||
| else | ||
| { | ||
| BOOST_ERROR("A C++ exception was thrown for which " | ||
| "there was no exception handler registered."); | ||
| } | ||
| } | ||
|
|
||
| // Finish sub interpreter | ||
| Py_EndInterpreter(sub_thread_state); | ||
|
|
||
| // Lock GIL again after for main interpreter | ||
| PyEval_AcquireThread(main_tstate); | ||
| // Release GIL | ||
| PyEval_ReleaseThread(main_tstate); | ||
| // Delete sub thread state | ||
| PyThreadState_Delete(main_tstate); | ||
| } | ||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
| BOOST_TEST(argc == 1); | ||
|
|
||
| // Register the module with the interpreter | ||
| if (PyImport_AppendInittab(const_cast<char*>("multi_thread_import_"), | ||
| #if PY_VERSION_HEX >= 0x03000000 | ||
| PyInit_multi_thread_import_ | ||
| #else | ||
| multi_thread_import_ | ||
| #endif | ||
| ) == -1) | ||
| { | ||
| BOOST_ERROR("Failed to add import2_ to the interpreter's " | ||
| "builtin modules"); | ||
| } | ||
|
|
||
| // Initialize the interpreter | ||
| Py_Initialize(); | ||
|
|
||
| // Capture main interpreter state to pass it to sub threads | ||
| PyInterpreterState* main_interp = PyThreadState_Get()->interp; | ||
|
|
||
| const int num_threads = 5; | ||
| std::vector<std::thread> threads; | ||
|
Check failure on line 138 in test/multi_thread_import_.cpp
|
||
|
|
||
| // Release the main thread's GIL | ||
| PyThreadState* main_thread_state = PyEval_SaveThread(); | ||
|
|
||
| for (int i = 0; i < num_threads; ++i) | ||
| { | ||
| threads.emplace_back(thread_func, i, main_interp); | ||
| } | ||
|
|
||
| for (auto& t : threads) | ||
| { | ||
| t.join(); | ||
| } | ||
|
|
||
| // Restore the main thread's GIL | ||
| PyEval_RestoreThread(main_thread_state); | ||
|
|
||
| // Boost.Python doesn't support Py_Finalize yet. | ||
| // Py_Finalize(); | ||
| return boost::report_errors(); | ||
| } | ||
|
|
||
| // Including this file makes sure | ||
| // that on Windows, any crashes (e.g. null pointer dereferences) invoke | ||
| // the debugger immediately, rather than being translated into structured | ||
| // exceptions that can interfere with debugging. | ||
| #include "module_tail.cpp" | ||
Oops, something went wrong.
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.
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.
Is this actually true?
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.
Same line is still in
test/import_.cpp.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.
It was authored before git migration and that was in 2007...