diff --git a/include/openPMD/binding/python/Pickle.hpp b/include/openPMD/binding/python/Pickle.hpp index 3d3b233eb4..5f3424f98f 100644 --- a/include/openPMD/binding/python/Pickle.hpp +++ b/include/openPMD/binding/python/Pickle.hpp @@ -67,9 +67,38 @@ add_pickle(pybind11::class_ &cl, T_SeriesAccessor &&seriesAccessor) std::vector const group = t[1].cast >(); - openPMD::Series series( - filename, Access::READ_ONLY, "defer_iteration_parsing = true"); - return seriesAccessor(std::move(series), group); + /* + * Cache the Series per thread. + */ + thread_local std::optional series; + bool re_initialize = [&]() { + try + { + return !series.has_value() || !series->operator bool() || + series->myPath().filePath() != filename; + } + /* + * Better safe than sorry, if anything goes wrong because the + * Series is in a weird state, just reinitialize it. + */ + catch (...) + { + return true; + } + }(); + if (re_initialize) + { + /* + * Do NOT close the old Series, it might still be active in + * terms of handed-out handles. + */ + series = std::make_optional( + filename, + Access::READ_ONLY, + "defer_iteration_parsing = true"); + } + + return seriesAccessor(*series, group); })); } } // namespace openPMD diff --git a/test/SerialIOTest.cpp b/test/SerialIOTest.cpp index feaab30788..c65c098c85 100644 --- a/test/SerialIOTest.cpp +++ b/test/SerialIOTest.cpp @@ -177,6 +177,7 @@ void write_and_read_many_iterations( { Series write(filename, Access::CREATE); + REQUIRE(write.myPath().filePath() == filename); for (unsigned int i = 0; i < nIterations; ++i) { // std::cout << "Putting iteration " << i << std::endl; @@ -1850,6 +1851,9 @@ inline void fileBased_write_test(const std::string &backend) "../samples/subdir/serial_fileBased_write%03T." + backend, Access::CREATE, jsonCfg); + REQUIRE( + o.myPath().filePath() == + ("../samples/subdir/serial_fileBased_write%03T." + backend)); ParticleSpecies &e_1 = o.iterations[1].particles["e"];