title: What is the correct way to combine async-for with an if condition to break mid await?
body: <p>If I have a coroutine that's consuming items from an async generator what is the &quot;best&quot; way to terminate that loop from an external condition?</p>
  <p>Consider this,</p>
  <pre class="lang-py prettyprint-override"><code>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)
  </code></pre>
  <p>If I set <code>shutdown_event</code> it will break out of the while loop, but not until the next <code>message</code> has been handled by the <code>async for</code> loop. What is the correct way to structure the <code>async for</code> iterator such that it can short circuit if a condition has been met between it yielding results?</p>
  <p>Is there a standard way to add a <code>Timeout</code>?</p>