title: Synchronous generator in asyncio
body: <p>I have the following scenario:</p>
  
  <ol>
  <li>I have a blocking, synchronous generator</li>
  <li>I have an non-blocking, async function</li>
  </ol>
  
  <p>I would like to run blocking generator (executed in a <code>ThreadPool</code>) and the <code>async</code> function on the event loop. How do I achieve this?</p>
  
  <p>The following function simply prints the output from the generator, not from <code>sleep</code> function.</p>
  
  <p>Thanks!</p>
  
  <pre><code>from concurrent.futures import ThreadPoolExecutor
  
  import numpy as np
  import asyncio
  import time
  
  
  def f():
      while True:
          r = np.random.randint(0, 3)
          time.sleep(r)
          yield r
  
  
  async def gen():
      loop = asyncio.get_event_loop()
      executor = ThreadPoolExecutor()
      gen = await loop.run_in_executor(executor, f)
      for item in gen:
          print(item)
          print('Inside generator')
  
  
  async def sleep():
      while True:
          await asyncio.sleep(1)
          print('Inside async sleep')
  
  
  async def combine():
      await asyncio.gather(sleep(), gen())
  
  
  def main():
      loop = asyncio.get_event_loop()
      loop.run_until_complete(combine())
  
  
  if __name__ == '__main__':
      main()
  </code></pre>