Hi,
I’m new to Trio and was looking for some help.
I have a generator that is ending via raising StopIteration
and I want to catch the exception and shutdown elegantly, the following code works:
async def func_called_by_run(self) -> None:
# ... Startup code
while True:
gen = await ... # returns a generator
try:
consume(gen) # Throws `StopIteration` when `gen` runs out.
except RuntimeError as e:
if e.args == ('generator raised StopIteration', ):
return
raise e
Is there a better way?
The above is very fragile because the error message ('generator raised StopIteration', )
could change.
Thanks in advance for any help.
Howard.