Skip to content

Commit

Permalink
examples/deepzoom: share a tile cache among all multiserver slides
Browse files Browse the repository at this point in the history
  • Loading branch information
bgilbert committed Aug 1, 2021
1 parent 8b3a8ee commit 24871eb
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions examples/deepzoom/deepzoom_multiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
else:
import openslide

from openslide import OpenSlide, OpenSlideError
from openslide import OpenSlide, OpenSlideCache, OpenSlideError
from openslide.deepzoom import DeepZoomGenerator

SLIDE_DIR = '.'
SLIDE_CACHE_SIZE = 10
SLIDE_TILE_CACHE_MB = 128
DEEPZOOM_FORMAT = 'jpeg'
DEEPZOOM_TILE_SIZE = 254
DEEPZOOM_OVERLAP = 1
Expand All @@ -60,11 +61,13 @@


class _SlideCache:
def __init__(self, cache_size, dz_opts):
def __init__(self, cache_size, tile_cache_mb, dz_opts):
self.cache_size = cache_size
self.dz_opts = dz_opts
self._lock = Lock()
self._cache = OrderedDict()
# Share a single tile cache among all slide handles
self._tile_cache = OpenSlideCache(tile_cache_mb * 1024 * 1024)

def get(self, path):
with self._lock:
Expand All @@ -75,6 +78,7 @@ def get(self, path):
return slide

osr = OpenSlide(path)
osr.set_cache(self._tile_cache)
slide = DeepZoomGenerator(osr, **self.dz_opts)
try:
mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
Expand Down Expand Up @@ -121,7 +125,9 @@ def _setup():
'DEEPZOOM_LIMIT_BOUNDS': 'limit_bounds',
}
opts = {v: app.config[k] for k, v in config_map.items()}
app.cache = _SlideCache(app.config['SLIDE_CACHE_SIZE'], opts)
app.cache = _SlideCache(
app.config['SLIDE_CACHE_SIZE'], app.config['SLIDE_TILE_CACHE_MB'], opts
)


def _get_slide(path):
Expand Down

0 comments on commit 24871eb

Please sign in to comment.