title: How to code consumer.producer with python asyncio?
body: <p>My Python version is 3.6.1.</p>
  
  <p>I wrote something to implement a model of consumer-producer with Python asyncio.
  But it doesn't work as expected.</p>
  
  <p>Four events all created but none of any print export.</p>
  
  <pre class="lang-py prettyprint-override"><code>async def consumer(queue, id):
      while True:
          val = await queue.get()
          print('{} get a val: {}'.format(id, val))
          await asyncio.sleep(1)
  
  async def producer(queue, id):
      for i in range(5):
          val = random.randint(1, 10)
          await queue.put(val)
          print('{} put a val: {}'.format(id, val))
          await asyncio.sleep(1)
  
  async def main():
      queue = asyncio.Queue()
  
      consumer_1 = asyncio.ensure_future(consumer(queue, 'consumer_1'))
      consumer_2 = asyncio.ensure_future(consumer(queue, 'consumer_2'))
  
      producer_1 = asyncio.ensure_future(producer(queue, 'producer_1'))
      producer_2 = asyncio.ensure_future(producer(queue, 'producer_2'))
  
      await asyncio.sleep(10)
      consumer_1.cancel()
      consumer_2.cancel()
  
      await asyncio.gather(consumer_1, consumer_2, producer_1, producer_2, return_exceptions=True)
  
  loop = asyncio.get_event_loop()
  tasks = [asyncio.ensure_future(main())]
  loop.run_until_complete(asyncio.wait(tasks))
  loop.close()
  </code></pre>
  
  <p>Could U please help me correct?</p>