Skip to content

Commit

Permalink
ExperimentAxisQuery: use Experiment.context.threadpool
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-williams committed Nov 19, 2024
1 parent 710489a commit 3017ad8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 51 deletions.
24 changes: 2 additions & 22 deletions apis/python/src/tiledbsoma/_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def __init__(
index_factory=index_factory,
)
self._index_factory = index_factory
self._threadpool_: Optional[ThreadPoolExecutor] = None

def obs(
self,
Expand Down Expand Up @@ -477,20 +476,7 @@ def to_anndata(
# Context management

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
pass

def __enter__(self) -> Self:
return self
Expand Down Expand Up @@ -775,13 +761,7 @@ def _threadpool(self) -> ThreadPoolExecutor:
"""
Returns the threadpool provided by the experiment's context.
If not available, creates a thread pool just in time."""
context = self.experiment.context
if context and context.threadpool:
return context.threadpool

if self._threadpool_ is None:
self._threadpool_ = ThreadPoolExecutor()
return self._threadpool_
return self.experiment.context.threadpool


@attrs.define(frozen=True)
Expand Down
29 changes: 0 additions & 29 deletions apis/python/tests/test_experiment_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,35 +540,6 @@ 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: Experiment):
"""
Verify 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
assert query.to_anndata("raw") is not None
assert query._threadpool_ is not None

assert query._threadpool_ is None

with closing(soma_experiment.axis_query("RNA")) as query:
assert query.to_anndata("raw") is not None
assert query._threadpool_ is not None

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"])],
Expand Down

0 comments on commit 3017ad8

Please sign in to comment.