Skip to content

Commit

Permalink
Reuse python diskcache instances (#58)
Browse files Browse the repository at this point in the history
Each instance holds open file handles, and each time we instantiate a backend, we rack up open file handles until we run out of them.

Test plan: Use this to successfully load the osmFISH data set in a python notebook.
  • Loading branch information
ttung authored Sep 5, 2018
1 parent f4087ce commit dae184e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="slicedimage",
version="0.0.4",
version="0.0.5",
description="Library to access sliced imaging data",
author="Tony Tung",
author_email="[email protected]",
Expand Down
10 changes: 8 additions & 2 deletions slicedimage/backends/_caching.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import io
from threading import Lock

from diskcache import Cache

Expand All @@ -11,10 +12,15 @@


class CachingBackend(Backend):
_LOCK = Lock()
_CACHE = {}

def __init__(self, cacheroot, authoritative_backend):
self._cacheroot = cacheroot
with CachingBackend._LOCK:
if cacheroot not in CachingBackend._CACHE:
CachingBackend._CACHE[cacheroot] = Cache(cacheroot, size=int(SIZE_LIMIT))
self._cache = CachingBackend._CACHE[cacheroot]
self._authoritative_backend = authoritative_backend
self._cache = Cache(cacheroot, size_limit=int(SIZE_LIMIT))

def read_contextmanager(self, name, checksum_sha256=None):
if checksum_sha256 is not None:
Expand Down

0 comments on commit dae184e

Please sign in to comment.