Skip to content

Commit

Permalink
Make framelist loading thread-safe
Browse files Browse the repository at this point in the history
This is so that `__getitem__` is thread-safe again. Only one thread
should load the framelist, and the other threads wait for it to finish.

Signed-off-by: Patrick Avery <[email protected]>
  • Loading branch information
psavery committed Sep 5, 2024
1 parent e9017ee commit 86ad798
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions hexrd/imageseries/load/framecache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Adapter class for frame caches
"""
import os
from threading import Lock

import numpy as np
from scipy.sparse import csr_matrix
Expand All @@ -23,6 +24,9 @@ def __init__(self, fname, style='npz', **kwargs):
"""
self._fname = fname
self._framelist = []
self._framelist_was_loaded = False
self._load_framelist_lock = Lock()

if style.lower() in ('yml', 'yaml', 'test'):
self._from_yml = True
self._load_yml()
Expand Down Expand Up @@ -95,11 +99,6 @@ def _load_framelist(self):
dtype=self._dtype)
self._framelist.append(frame)

@property
def _framelist_was_loaded(self):
# Just assume that if the framelist is empty, it wasn't loaded...
return len(self._framelist) > 0

@property
def metadata(self):
"""(read-only) Image sequence metadata
Expand Down Expand Up @@ -131,10 +130,20 @@ def dtype(self):
def shape(self):
return self._shape

def __getitem__(self, key):
def _load_framelist_if_needed(self):
if not self._framelist_was_loaded:
# Load the framelist now
self._load_framelist()
# Only one thread should load the framelist.
# Acquire the lock for loading the framelist.
with self._load_framelist_lock:
# It is possible that another thread already loaded
# the framelist by the time this lock was acquired.
# Check again.
if not self._framelist_was_loaded:
self._load_framelist()
self._framelist_was_loaded = True

def __getitem__(self, key):
self._load_framelist_if_needed()
return self._framelist[key].toarray()

def __iter__(self):
Expand Down

0 comments on commit 86ad798

Please sign in to comment.