diff --git a/tiled/_tests/test_pickle.py b/tiled/_tests/test_pickle.py index 8585317d1..0de6db3c3 100644 --- a/tiled/_tests/test_pickle.py +++ b/tiled/_tests/test_pickle.py @@ -8,6 +8,7 @@ from packaging.version import parse from ..client import from_context +from ..client.cache import Cache from ..client.context import Context MIN_VERSION = "0.1.0a104" @@ -24,7 +25,7 @@ def test_pickle_context(): @pytest.mark.parametrize("structure_clients", ["numpy", "dask"]) -def test_pickle_clients(structure_clients): +def test_pickle_clients(structure_clients, tmpdir): try: httpx.get(API_URL).raise_for_status() except Exception: @@ -34,7 +35,8 @@ def test_pickle_clients(structure_clients): raise pytest.skip( f"Server at {API_URL} is running too old a version to test against." ) - client = from_context(context, structure_clients) + cache = Cache(tmpdir / "http_response_cache.db") + client = from_context(context, structure_clients, cache) pickle.loads(pickle.dumps(client)) for segements in [ ["generated"], diff --git a/tiled/client/cache.py b/tiled/client/cache.py index 44d85be20..9d05e27e1 100644 --- a/tiled/client/cache.py +++ b/tiled/client/cache.py @@ -12,7 +12,7 @@ import appdirs import httpx -from .utils import TiledResponse +from .utils import SerializableLock, TiledResponse CACHE_DATABASE_SCHEMA_VERSION = 1 @@ -200,7 +200,7 @@ def __init__( self._filepath = filepath self._owner_thread = threading.current_thread().ident self._conn = _prepare_database(filepath, readonly) - self._lock = threading.Lock() + self._lock = SerializableLock() def __repr__(self): return f"<{type(self).__name__} {str(self._filepath)!r}>" @@ -219,16 +219,23 @@ def write_safe(self): return is_main_thread or sqlite_is_safe def __getstate__(self): - return (self.filepath, self.capacity, self.max_item_size, self._readonly) + return ( + self.filepath, + self.capacity, + self.max_item_size, + self._readonly, + self._lock, + ) def __setstate__(self, state): - (filepath, capacity, max_item_size, readonly) = state + (filepath, capacity, max_item_size, readonly, lock) = state self._capacity = capacity self._max_item_size = max_item_size self._readonly = readonly self._filepath = filepath self._owner_thread = threading.current_thread().ident self._conn = _prepare_database(filepath, readonly) + self._lock = lock @property def readonly(self):