diff --git a/CHANGELOG.md b/CHANGELOG.md index d6814b8b7..318289ecc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Improvements +- Better release bioformats resources (#502) + ## Version 1.3.2 ### Improvements diff --git a/sources/bioformats/large_image_source_bioformats/__init__.py b/sources/bioformats/large_image_source_bioformats/__init__.py index b77000d95..8e381c22e 100644 --- a/sources/bioformats/large_image_source_bioformats/__init__.py +++ b/sources/bioformats/large_image_source_bioformats/__init__.py @@ -32,7 +32,6 @@ import math import numpy import os -import six import threading import types @@ -54,11 +53,25 @@ _javabridgeStarted = None +_openImages = [] def _monitor_thread(): main_thread = threading.main_thread() main_thread.join() + if len(_openImages): + try: + javabridge.attach() + while len(_openImages): + source = _openImages.pop() + try: + source._bioimage.close() + except Exception: + pass + source._bioimage = None + finally: + if javabridge.get_env(): + javabridge.detach() _stopJavabridge() @@ -91,8 +104,7 @@ def _stopJavabridge(*args, **kwargs): _javabridgeStarted = None -@six.add_metaclass(LruCacheMetaclass) -class BioformatsFileTileSource(FileTileSource): +class BioformatsFileTileSource(FileTileSource, metaclass=LruCacheMetaclass): """ Provides tile access to via Bioformats. """ @@ -150,6 +162,7 @@ def __init__(self, path, **kwargs): # noqa except AttributeError as exc: self._logger.debug('File cannot be opened via Bioformats. (%r)' % exc) raise TileSourceException('File cannot be opened via Bioformats. (%r)' % exc) + _openImages.append(self) rdr = self._bioimage.rdr # Bind additional functions not done by bioformats module. @@ -245,6 +258,7 @@ def __del__(self): try: javabridge.attach() self._bioimage.close() + _openImages.remove(self) finally: if javabridge.get_env(): javabridge.detach() @@ -265,7 +279,7 @@ def _getSeriesStarts(self, rdr): frameList = [] nextSeriesNum = 0 try: - for key, value in six.iteritems(seriesMetadata): + for key, value in seriesMetadata.items(): frameNum = int(value) seriesNum = int(key.split('Series ')[1].split('|')[0]) - 1 if seriesNum >= 0 and seriesNum < self._metadata['seriesCount']: