Skip to content

Commit

Permalink
Use SerializableLock
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Feb 22, 2024
1 parent 0d1e8ff commit 68f1f10
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions tiled/_tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand All @@ -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"],
Expand Down
15 changes: 11 additions & 4 deletions tiled/client/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import appdirs
import httpx

from .utils import TiledResponse
from .utils import SerializableLock, TiledResponse

CACHE_DATABASE_SCHEMA_VERSION = 1

Expand Down Expand Up @@ -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}>"
Expand All @@ -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):
Expand Down

0 comments on commit 68f1f10

Please sign in to comment.