Wow, you've come across an interesting corner case! By using asyncio.sleep() you have triggered some interesting behavior.

I think you might have uncovered a bug in Twisted's integration with the Python 3 asyncioreactor and async/await. You may wish to follow up with the Twisted developer's on the Twisted mailing list.

I'm not 100% sure, but here are my thoughts.

The implementation of asyncio.sleep() is tightly coupled to the Python 3 asyncio implementation. It uses the asyncio.Future (which is similar to Twisted's deferred), and it uses get_event_loop() (which is similar to Twisted's reactor).

asyncio.sleep is implemented like this:

@coroutine def sleep(delay, result=None, *, loop=None): """Coroutine that completes after a given time (in seconds).""" if delay == 0: yield return result if loop is None: loop = events.get_event_loop() future = loop.create_future() h = future._loop.call_later(delay, futures._set_result_unless_cancelled, future, result) try: return (yield from future) finally: h.cancel()

I changed your code example slightly to pass Twisted's asyncioreactor event loop into asyncio.sleep():

from twisted.internet import asyncioreactor asyncioreactor.install() from twisted.internet import reactor 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, loop=reactor._asyncioEventloop) print("done sleep") return 42 @task.react def main(reactor): d = ensureDeferred(sleepy(reactor)) d.addCallback(print) return d

I still got the same error as you: builtins.AssertionError: yield from wasn't used with future

The stack trace looks like:

main function encountered error Traceback (most recent call last): File "b.py", line 16, in <module> @task.react File "/Users/crodrigues/twisted8/src/twisted/internet/task.py", line 908, in react finished = main(_reactor, *argv) File "b.py", line 19, in main d = ensureDeferred(sleepy(reactor)) File "/Users/crodrigues/twisted8/src/twisted/internet/defer.py", line 823, in ensureDeferred return _inlineCallbacks(None, coro, Deferred()) --- <exception caught here> --- File "/Users/crodrigues/twisted8/src/twisted/internet/defer.py", line 1301, in _inlineCallbacks result = g.send(result) File "b.py", line 12, in sleepy await asyncio.sleep(3, loop=reactor._asyncioEventloop) File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 478, in sleep return (yield from future) builtins.AssertionError: yield from wasn't used with future

I think the asyncio.sleep() is a coroutine that is supposed to run to completion on the asyncio loop, but this is not happening here, hence the assertion.

I think the problem is being introduced by result = g.send(result). I'm not sure you can send() to a coroutine like this and expect it to work.

I advise you to ask on the Twisted mailing list to get more detailed feedback.