| 70 | } |
| 71 | |
| 72 | void finalize() |
| 73 | { |
| 74 | |
| 75 | // Thread-safe finalization with double-checked locking pattern |
| 76 | if (is_active()) { |
| 77 | static std::mutex m; |
| 78 | std::lock_guard<std::mutex> lock(m); |
| 79 | if (is_active()) { |
| 80 | |
| 81 | // Take GIL and finalize Python session |
| 82 | if (init_thread_state != nullptr) { |
| 83 | PyEval_RestoreThread(init_thread_state); |
| 84 | init_thread_state = nullptr; |
| 85 | } |
| 86 | Py_Finalize(); |
| 87 | |
| 88 | // Check that Python session has been finalized |
| 89 | if (is_active()) { |
| 90 | LBANN_WARNING("error finalizing embedded Python session"); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | bool is_active() { return Py_IsInitialized(); } |
| 97 | |