Skip to content

Commit

Permalink
add lock=Lock() to cachetools cached instance to avoid thread cor…
Browse files Browse the repository at this point in the history
…ruption (#238)

* add `lock=Lock()` to cachetools `cached` instance to avoid thread corruption

* update python version support
  • Loading branch information
vincentsarago authored Feb 13, 2025
1 parent c13cf41 commit ff47e90
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ on:
- '*'
pull_request:
env:
LATEST_PY_VERSION: '3.12'
LATEST_PY_VERSION: '3.13'

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Unreleased

## 8.1.0 (2025-02-13)

* add `lock=Lock()` to cachetools `cached` instance to avoid thread corruption
* remove python 3.8 support
* add python 3.13 support

## 8.0.0 (2024-10-21)

* remove deprecated methods
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/az.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""cogeo-mosaic Azure Blob Storage backend."""

import json
from threading import Lock
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -68,6 +69,7 @@ def write(self, overwrite: bool = False, **kwargs: Any):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get mosaicjson document."""
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import abc
import itertools
import warnings
from threading import Lock
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, Union

import attr
Expand Down Expand Up @@ -235,6 +236,7 @@ def assets_for_bbox(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, x, y, z: hashkey(self.input, x, y, z, self.mosaicid),
lock=Lock(),
)
def get_assets(self, x: int, y: int, z: int) -> List[str]:
"""Find assets."""
Expand Down
3 changes: 3 additions & 0 deletions cogeo_mosaic/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import warnings
from decimal import Decimal
from threading import Lock
from typing import Any, Dict, List, Sequence
from urllib.parse import urlparse

Expand Down Expand Up @@ -88,6 +89,7 @@ def __attrs_post_init__(self):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get Mosaic definition info."""
Expand Down Expand Up @@ -218,6 +220,7 @@ def update(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, x, y, z: hashkey(self.input, x, y, z, self.mosaicid),
lock=Lock(),
)
def get_assets(self, x: int, y: int, z: int) -> List[str]:
"""Find assets."""
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import pathlib
from threading import Lock

import attr
from cachetools import TTLCache, cached
Expand Down Expand Up @@ -39,6 +40,7 @@ def write(self, overwrite: bool = False):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get mosaicjson document."""
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/gs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""cogeo-mosaic Google Cloud Storage backend."""

import json
from threading import Lock
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -60,6 +61,7 @@ def write(self, overwrite: bool = False, **kwargs: Any):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get mosaicjson document."""
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/s3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""cogeo-mosaic AWS S3 backend."""

import json
from threading import Lock
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -58,6 +59,7 @@ def write(self, overwrite: bool = False, **kwargs: Any):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get mosaicjson document."""
Expand Down
3 changes: 3 additions & 0 deletions cogeo_mosaic/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sqlite3
import warnings
from pathlib import Path
from threading import Lock
from typing import Dict, List, Sequence
from urllib.parse import urlparse

Expand Down Expand Up @@ -94,6 +95,7 @@ def __exit__(self, exc_type, exc_value, traceback):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get Mosaic definition info."""
Expand Down Expand Up @@ -311,6 +313,7 @@ def update(
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self, x, y, z: hashkey(self.input, x, y, z, self.mosaicid),
lock=Lock(),
)
def get_assets(self, x: int, y: int, z: int) -> List[str]:
"""Find assets."""
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
from threading import Lock
from typing import Dict, List, Optional, Sequence, Type

import attr
Expand Down Expand Up @@ -151,6 +152,7 @@ def query_from_link(link: Dict, query: Dict):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda url, query, **kwargs: hashkey(url, json.dumps(query), **kwargs),
lock=Lock(),
)
def _fetch( # noqa: C901
stac_url: str,
Expand Down
2 changes: 2 additions & 0 deletions cogeo_mosaic/backends/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import json
from threading import Lock
from typing import Dict, Sequence

import attr
Expand Down Expand Up @@ -32,6 +33,7 @@ class HttpBackend(BaseBackend):
@cached( # type: ignore
TTLCache(maxsize=cache_config.maxsize, ttl=cache_config.ttl),
key=lambda self: hashkey(self.input),
lock=Lock(),
)
def _read(self) -> MosaicJSON: # type: ignore
"""Get mosaicjson document."""
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "cogeo-mosaic"
description = 'CLI and Backends to work with MosaicJSON.'
requires-python = ">=3.8"
requires-python = ">=3.9"
license = {file = "LICENSE"}
authors = [
{ name = "Vincent Sarago", email = "vincent@developmentseed.com" },
Expand All @@ -13,11 +13,11 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Scientific/Engineering :: GIS",
]
dynamic = ["version", "readme"]
Expand Down

0 comments on commit ff47e90

Please sign in to comment.