From d47adcf23896e846d69f0f511efae2da86c4d1ef Mon Sep 17 00:00:00 2001 From: John Kerl Date: Fri, 20 Dec 2024 10:51:35 -0500 Subject: [PATCH] Restore `ExperimentAxisQuery.close`, minimally --- apis/python/src/tiledbsoma/_query.py | 22 ++++++++++++++++ apis/python/tests/test_experiment_query.py | 30 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/apis/python/src/tiledbsoma/_query.py b/apis/python/src/tiledbsoma/_query.py index 8e6849c187..f1c337af43 100644 --- a/apis/python/src/tiledbsoma/_query.py +++ b/apis/python/src/tiledbsoma/_query.py @@ -242,6 +242,28 @@ def __init__( ) self._index_factory = index_factory + def close(self) -> None: + pass + + # WIP + # To be restored from somacore 1.0.17: + # + # def close(self) -> None: + # """Releases resources associated with this query. + # + # This method must be idempotent. + # + # Lifecycle: maturing + # """ + # # Because this may be called during ``__del__`` when we might be getting + # # disassembled, sometimes ``_threadpool_`` is simply missing. + # # Only try to shut it down if it still exists. + # pool = getattr(self, "_threadpool_", None) + # if pool is None: + # return + # pool.shutdown() + # self._threadpool_ = None + def obs( self, *, diff --git a/apis/python/tests/test_experiment_query.py b/apis/python/tests/test_experiment_query.py index e70605cf2e..a5f0290354 100644 --- a/apis/python/tests/test_experiment_query.py +++ b/apis/python/tests/test_experiment_query.py @@ -545,6 +545,36 @@ def test_error_corners(soma_experiment: Experiment): next(query.varp(lyr_name)) +@pytest.mark.parametrize("n_obs,n_vars", [(1001, 99)]) +def test_query_cleanup(soma_experiment: soma.Experiment): + """ + Verify soma.Experiment.query works as context manager and stand-alone, + and that it cleans up correctly. + """ + from contextlib import closing + + # Forces a context without a thread pool, which in turn causes ExperimentAxisQuery + # to own (and release) its own thread pool. + context = SOMATileDBContext() + context.threadpool = None + soma_experiment = get_soma_experiment_with_context(soma_experiment, context) + + with soma_experiment.axis_query("RNA") as query: + assert query.n_obs == 1001 + assert query.n_vars == 99 + # WIP assert query.to_anndata("raw") is not None + # WIP assert query._threadpool_ is not None + + # WIP assert query._threadpool_ is None + + with closing(soma_experiment.axis_query("RNA")) as query: + pass + # WIP assert query.to_anndata("raw") is not None + # WIP assert query._threadpool_ is not None + + # WIP assert query._threadpool_ is None + + @pytest.mark.parametrize( "n_obs,n_vars,obsp_layer_names,varp_layer_names,obsm_layer_names,varm_layer_names", [(1001, 99, ["foo"], ["bar"], ["baz"], ["quux"])],