title: How can I run asyncio library code on top of Twisted's asyncioreactor?
body: <p>I've managed to import/install Twisted's <code>asyncioreactor</code> and execute a trivial asynchronous function:</p>
  
  <pre><code>from twisted.internet import asyncioreactor
  asyncioreactor.install()
  from twisted.internet import task
  from twisted.internet.defer import inlineCallbacks
  from twisted.internet.defer import ensureDeferred
  
  async def sleepy(reactor):
      print("SLEEPING")
      await task.deferLater(reactor, 3.0, lambda: None)
      print("done sleep")
      return 42
  
  @task.react
  def main(reactor):
      d = ensureDeferred(sleepy(reactor))
      d.addCallback(print)
      return d
  </code></pre>
  
  <p>I'd like to intermix an asyncio library in said code, for instance <code>asyncio.sleep</code>.  I've tried the following:</p>
  
  <pre><code>from twisted.internet import asyncioreactor
  asyncioreactor.install()
  from twisted.internet import task
  from twisted.internet.defer import inlineCallbacks
  from twisted.internet.defer import ensureDeferred
  
  import asyncio
  
  async def sleepy(reactor):
      print("SLEEPING")
      await asyncio.sleep(3)
      print("done sleep")
      return 42
  
  @task.react
  def main(reactor):
      d = ensureDeferred(sleepy(reactor))
      d.addCallback(print)
      return d
  </code></pre>
  
  <p>which produces the following error:</p>
  
  <pre><code> $ python test.py
  SLEEPING
  main function encountered error
  Traceback (most recent call last):
    File "test.py", line 16, in &lt;module&gt;
      @task.react
    File "/Users/blz/.pyenv/versions/3.6.0/lib/python3.6/site-packages/twisted/internet/task.py", line 908, in react
      finished = main(_reactor, *argv)
    File "test.py", line 18, in main
      d = ensureDeferred(sleepy(reactor))
    File "/Users/blz/.pyenv/versions/3.6.0/lib/python3.6/site-packages/twisted/internet/defer.py", line 823, in ensureDeferred
      return _inlineCallbacks(None, coro, Deferred())
  --- &lt;exception caught here&gt; ---
    File "/Users/blz/.pyenv/versions/3.6.0/lib/python3.6/site-packages/twisted/internet/defer.py", line 1301, in _inlineCallbacks
      result = g.send(result)
    File "test.py", line 11, in sleepy
      await asyncio.sleep(3)
    File "/Users/blz/.pyenv/versions/3.6.0/lib/python3.6/asyncio/tasks.py", line 476, in sleep
      return (yield from future)
  builtins.AssertionError: yield from wasn't used with future
  </code></pre>
  
  <p>Fair enough, thought I, so I tried swapping <code>await asyncio.sleep(3)</code> with <code>await ensureDeferred(asyncio.sleep(3))</code> and <code>await asyncio.ensure_future(asyncio.sleep(3))</code>, but I get exactly the same error.</p>
  
  <h3>How can I schedule an aio coroutine (and/or <code>Future</code>) to run on the same event loop as is used by <code>asyncioreactor</code>?</h3>