Skip to content

Commit

Permalink
Clean up generator-local thread
Browse files Browse the repository at this point in the history
  • Loading branch information
ejguan committed Feb 7, 2023
1 parent d430053 commit 603fe31
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions torchdata/datapipes/iter/util/prefetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ def __iter__(self):
try:
prefetch_data = _PrefetchData(self.source_datapipe, self.buffer_size)
self.prefetch_data = prefetch_data
self.thread = threading.Thread(
thread = threading.Thread(
target=PrefetcherIterDataPipe.thread_worker, args=(prefetch_data,), daemon=True
)
self.thread = thread
self.thread.start()
while prefetch_data.run_prefetcher:
if len(prefetch_data.prefetch_buffer) > 0:
Expand All @@ -99,9 +100,7 @@ def __iter__(self):
time.sleep(CONSUMER_SLEEP_INTERVAL)
finally:
prefetch_data.run_prefetcher = False
if self.thread is not None:
self.thread.join()
self.thread = None
thread.join()

def __getstate__(self):
"""
Expand All @@ -124,3 +123,4 @@ def reset(self):
if self.thread is not None:
self.prefetch_data.run_prefetcher = False
self.thread.join()
self.thread = None

0 comments on commit 603fe31

Please sign in to comment.