Skip to content

Commit

Permalink
Handle terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
ejguan committed Feb 15, 2023
1 parent e81a430 commit d3863ed
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions torchdata/datapipes/iter/util/prefetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ def __iter__(self):
thread.start()
self.thread = thread

# Lazily import to prevent circular import
from torchdata.dataloader2 import communication

while not prefetch_data.stop_iteration or len(prefetch_data.prefetch_buffer) > 0:
if len(prefetch_data.prefetch_buffer) > 0:
data = prefetch_data.prefetch_buffer.popleft()
if isinstance(data, Exception):
if isinstance(data, StopIteration):
if isinstance(data, (StopIteration, communication.iter.TerminateRequired)):
break
raise data
yield data
Expand Down Expand Up @@ -227,11 +230,14 @@ def __iter__(self):
thread.start()
self.thread = thread

# Lazily import to prevent circular import
from torchdata.dataloader2 import communication

while not prefetch_data.stop_iteration or len(prefetch_data.prefetch_buffer) > 0:
if len(prefetch_data.prefetch_buffer) > 0:
data = prefetch_data.prefetch_buffer.popleft()
if isinstance(data, Exception):
if isinstance(data, StopIteration):
if isinstance(data, (StopIteration, communication.iter.TerminateRequired)):
break
raise data
yield data
Expand Down

0 comments on commit d3863ed

Please sign in to comment.