Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](reader): fix play-all may fail due to 'ValueError: generator already executing' #882

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions feeluown/utils/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Sequence,
AsyncIterable,
)
from threading import Lock

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -162,6 +163,7 @@ def __init__(self, g, count: Optional[int], offset: int = 0):
self._count = count
self.offset = offset
self._objects: List[T] = []
self._lock = Lock()

@property
def count(self):
Expand Down Expand Up @@ -189,7 +191,8 @@ def read(self, index):
def _read_next(self) -> T:
if self._count is None or self.offset < self.count:
try:
obj = next(self._g)
with self._lock:
obj = next(self._g)
except StopIteration:
if self._count is None:
self._count = self.offset + 1
Expand Down Expand Up @@ -218,6 +221,7 @@ def __init__(self,
self._ranges: List[Tuple[int, int]] = [] # list of tuple
self._objects: List[Optional[T]] = [None] * count
self._read_func = read_func
self._lock = Lock()

assert max_per_read > 0, 'max_per_read must big than 0'
self._max_per_read = max_per_read
Expand Down Expand Up @@ -269,7 +273,12 @@ def read_range(self, start, end) -> List[T]:
return cast(List[T], self._objects[start:end])

def _read_range(self, start, end):
# TODO: make this method thread safe
# Though this method is thread safe now, _read_range_unsafe may read
# the same range multiple times.
with self._lock:
self._read_range_unsafe(start, end)

def _read_range_unsafe(self, start, end):
assert start <= end, 'start should less than end'
logger.debug('trigger read_func(%d, %d)', start, end)
objs = list(self._read_func(start, end))
Expand Down
Loading