title: How to write your own async/awaitable coroutine function in Python?
body: <p>I'm trying to write my own awaiatbale function which could use in asyncio loop such as <code>asyncio.sleep()</code> method or something like these pre-awaitable implemented methods.</p>
  
  <p>Here is what I've done so far:</p>
  
  <pre><code>import asyncio
  
  def coro1():
      for i in range(1, 10):
          yield i
  
  def coro2():
      for i in range(1, 10):
          yield i*10
  
  class Coro:  # Not used.
      def __await__(self):
          for i in range(1, 10):
              yield i * 100
  
  @asyncio.coroutine
  def wrapper1():
      return (yield from coro1())
  
  @asyncio.coroutine
  def wrapper2():
      return (yield from coro2())
  
  for i in wrapper1():
      print(i)
  
  print("Above result was obvious which I can iterate around a couroutine.".center(80, "#"))
  
  async def async_wrapper():
      await wrapper1()
      await wrapper2()
  
  loop = asyncio.get_event_loop()
  futures = [asyncio.ensure_future(async_wrapper())]
  result = loop.run_until_complete(asyncio.gather(*futures))
  print(result)
  
  loop.close()
  </code></pre>
  
  <h2>What I got as a result:</h2>
  
  <pre><code>1
  2
  3
  4
  5
  6
  7
  8
  9
  #######Above result was obvious which I can iterate around a couroutine.#########
  Traceback (most recent call last):
    File "stack-coroutine.py", line 36, in &lt;module&gt;
      result = loop.run_until_complete(asyncio.gather(*futures))
    File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
      return future.result()
    File "stack-coroutine.py", line 30, in async_wrapper
      await wrapper1()
    File "stack-coroutine.py", line 18, in wrapper1
      return (yield from coro1())
    File "stack-coroutine.py", line 5, in coro1
      yield i
  RuntimeError: Task got bad yield: 1
  </code></pre>
  
  <h1>What I expect as a result:</h1>
  
  <pre><code>1
  10
  2
  20
  3
  30
  .
  .
  .
  </code></pre>
  
  <hr>
  
  <p>[<strong>NOTE</strong>]:</p>
  
  <ul>
  <li>I'm not looking for a multithread or multiprocess method.</li>
  <li>This <a href="https://stackoverflow.com/questions/44810614/asyncio-awaitable-object-basic-example">Question</a> is almost similar to my question which has not resolved yet.</li>
  <li>I'm using <em>Python3.6</em></li>
  </ul>