Skip to content

Commit

Permalink
Merge pull request #502 from girder/bioformats-release
Browse files Browse the repository at this point in the history
Better release bioformats resources.
  • Loading branch information
manthey authored Dec 16, 2020
2 parents 1e02eed + 8a12e94 commit 5a22556
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Improvements
- Better release bioformats resources (#502)

## Version 1.3.2

### Improvements
Expand Down
22 changes: 18 additions & 4 deletions sources/bioformats/large_image_source_bioformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import math
import numpy
import os
import six
import threading
import types

Expand All @@ -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()


Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -245,6 +258,7 @@ def __del__(self):
try:
javabridge.attach()
self._bioimage.close()
_openImages.remove(self)
finally:
if javabridge.get_env():
javabridge.detach()
Expand All @@ -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']:
Expand Down

0 comments on commit 5a22556

Please sign in to comment.