|
22 | 22 |
|
23 | 23 | #include "openPMD/IO/Access.hpp" |
24 | 24 | #include "openPMD/Series.hpp" |
| 25 | +#include "openPMD/auxiliary/StringManip.hpp" |
25 | 26 | #include "openPMD/backend/Attributable.hpp" |
26 | 27 |
|
27 | 28 | #include "Common.hpp" |
@@ -67,9 +68,40 @@ add_pickle(pybind11::class_<T_Args...> &cl, T_SeriesAccessor &&seriesAccessor) |
67 | 68 | std::vector<std::string> const group = |
68 | 69 | t[1].cast<std::vector<std::string> >(); |
69 | 70 |
|
70 | | - openPMD::Series series( |
71 | | - filename, Access::READ_ONLY, "defer_iteration_parsing = true"); |
72 | | - return seriesAccessor(std::move(series), group); |
| 71 | + /* |
| 72 | + * Cache the Series per thread. |
| 73 | + */ |
| 74 | + thread_local std::optional<openPMD::Series> series; |
| 75 | + bool re_initialize = [&]() { |
| 76 | + try |
| 77 | + { |
| 78 | + return !series.has_value() || !series->operator bool() || |
| 79 | + auxiliary::replace_all( |
| 80 | + series->myPath().filePath(), "\\", "/") != |
| 81 | + auxiliary::replace_all(filename, "\\", "/"); |
| 82 | + } |
| 83 | + /* |
| 84 | + * Better safe than sorry, if anything goes wrong because the |
| 85 | + * Series is in a weird state, just reinitialize it. |
| 86 | + */ |
| 87 | + catch (...) |
| 88 | + { |
| 89 | + return true; |
| 90 | + } |
| 91 | + }(); |
| 92 | + if (re_initialize) |
| 93 | + { |
| 94 | + /* |
| 95 | + * Do NOT close the old Series, it might still be active in |
| 96 | + * terms of handed-out handles. |
| 97 | + */ |
| 98 | + series = std::make_optional<Series>( |
| 99 | + filename, |
| 100 | + Access::READ_ONLY, |
| 101 | + "defer_iteration_parsing = true"); |
| 102 | + } |
| 103 | + |
| 104 | + return seriesAccessor(*series, group); |
73 | 105 | })); |
74 | 106 | } |
75 | 107 | } // namespace openPMD |
0 commit comments