Skip to content

Commit

Permalink
Merge pull request #140 from kshakir/ks_catch_generator_exception
Browse files Browse the repository at this point in the history
Catch and pass on exceptions from background generators.
  • Loading branch information
francois-a authored Aug 24, 2024
2 parents 3471680 + 00fef65 commit ada32f1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tensorqtl/genotypeio.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ def __init__(self, generator, max_prefetch=10):
self.start()

def run(self):
for item in self.generator:
self.queue.put(item)
try:
for item in self.generator:
self.queue.put(item)
except Exception as exception:
self.queue.put(exception)
self.queue.put(None)

def next(self):
next_item = self.queue.get()
if next_item is None:
self.join()
raise StopIteration
if isinstance(next_item, Exception):
self.join()
raise next_item
return next_item

def __next__(self):
Expand Down

0 comments on commit ada32f1

Please sign in to comment.