title: asyncio awaitable object - basic example
body: <p>I'm trying to understand how to make an awaitable object. The definition from the <a href="https://www.python.org/dev/peps/pep-0492/#await-expression" rel="noreferrer">documentation</a> states:</p>
  
  <blockquote>
    <p>An object with an __await__ method returning an iterator.</p>
  </blockquote>
  
  <p>Guided by that definition I wrote the sample code:</p>
  
  <pre><code>import asyncio
  
  async def produce_list():
          num = await Customer()
          print(num)
  
  class Customer(object):
  
      def __await__(self):
          return iter([1, 2, 3, 4])
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(produce_list())
  </code></pre>
  
  <p>The flow that I expected was:</p>
  
  <ol>
  <li>Event loop gives control to <code>produce_list()</code>. <code>produce_list()</code> gives up execution on <code>num = await Customer()</code>.</li>
  <li><code>Customer()</code> is executed and returns an iterator. Which because returns the first value in the iterator. <strong><em>Q1:</strong> am not clear here why <code>num</code> isn't becoming the iterator itself. Also what is doing a <code>send</code> here?</em></li>
  <li>Once the last value the iterator has been reached. <code>num = 4</code> the execution of the coroutine continues to <code>print(num)</code>, and prints the value 4.</li>
  </ol>
  
  <p>What I got:</p>
  
  <pre><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  ~/workspace/dashboard/so_question_await.py in &lt;module&gt;()
       16 
       17 loop = asyncio.get_event_loop()
  ---&gt; 18 loop.run_until_complete(produce_list())
  
  /usr/lib/python3.5/asyncio/base_events.py in run_until_complete(self, future)
      464             raise RuntimeError('Event loop stopped before Future completed.')
      465 
  --&gt; 466         return future.result()
      467 
      468     def stop(self):
  
  /usr/lib/python3.5/asyncio/futures.py in result(self)
      291             self._tb_logger = None
      292         if self._exception is not None:
  --&gt; 293             raise self._exception
      294         return self._result
      295 
  
  /usr/lib/python3.5/asyncio/tasks.py in _step(***failed resolving arguments***)
      239                 result = coro.send(None)
      240             else:
  --&gt; 241                 result = coro.throw(exc)
      242         except StopIteration as exc:
      243             self.set_result(exc.value)
  
  ~/workspace/dashboard/so_question_await.py in produce_list()
        5 
        6 async def produce_list():
  ----&gt; 7         num = await Customer()
        8         print(num)
        9 
  
  RuntimeError: Task got bad yield: 1
  </code></pre>
  
  <p>What concepts have I gotten wrong here? </p>
  
  <p>In the end I'm looking for an example that uses iteration through a list as an event to return to the control of the coroutine.</p>