If I have a coroutine that's consuming items from an async generator what is the "best" way to terminate that loop from an external condition?
Consider this,
while not self.shutdown_event.is_set():
async with self.external_lib_client as client:
async for message in client:
if self.shutdown_event.is_set():
break
await self.handle(message)
If I set shutdown_event it will break out of the while loop, but not until the next message has been handled by the async for loop. What is the correct way to structure the async for iterator such that it can short circuit if a condition has been met between it yielding results?
Is there a standard way to add a Timeout?