title: How to yield from an async for loop using asyncio?
body: <p>I'm trying to write a simple asynchronous data batch generator, but having troubles with understanding how to yield from an async for loop. Here I've written a simple class illustrating my idea:</p>
  
  <pre><code>import asyncio
  from typing import List
  
  class AsyncSimpleIterator:
      def __init__(self, data: List[str], batch_size=None):
          self.data = data
          self.batch_size = batch_size
          self.doc2index = self.get_doc_ids()
  
      def get_doc_ids(self):
          return list(range(len(self.data)))
  
      async def get_batch_data(self, doc_ids):
          print("get_batch_data() running")
          page = [self.data[j] for j in doc_ids]
          return page
  
      async def get_docs(self, batch_size):
          print("get_docs() running")
  
          _batch_size = self.batch_size or batch_size
          batches = [self.doc2index[i:i + _batch_size] for i in
                     range(0, len(self.doc2index), _batch_size)]
  
          for _, doc_ids in enumerate(batches):
              docs = await self.get_batch_data(doc_ids)
              yield docs, doc_ids
  
      async def main(self):
          print("main() running")
          async for res in self.get_docs(batch_size=2):
              print(res)  # how to yield instead of print?
  
      def gen_batches(self):
          # how to get results of self.main() here?
          loop = asyncio.get_event_loop()
          loop.run_until_complete(self.main())
          loop.close()
  
  
   DATA = ["Hello, world!"] * 4
   iterator = AsyncSimpleIterator(DATA)
   iterator.gen_batches()
  </code></pre>
  
  <p>So, my question is, <strong>how to yield a result from <code>main()</code> to gather it inside <code>gen_batches()</code></strong>? </p>
  
  <p>When I print the result inside <code>main()</code>, I get the following output:</p>
  
  <pre><code>main() running
  get_docs() running
  get_batch_data() running
  (['Hello, world!', 'Hello, world!'], [0, 1])
  get_batch_data() running
  (['Hello, world!', 'Hello, world!'], [2, 3])
  </code></pre>