How can I call prompt_toolkit from the tornado event loop?
<p>I am trying to use prompt_toolkit from an application that uses the tornado event loop, but I can not work out the correct way to add the prompt_toolkit prompt to the event loop.</p>

<p>The prompt_toolkit documentation has an example of using it in asyncio (<a href="http://python-prompt-toolkit.readthedocs.io/en/stable/pages/building_prompts.html#prompt-in-an-asyncio-application" rel="nofollow">Asyncio Docs</a>):</p>

<pre><code>from prompt_toolkit.shortcuts import prompt_async

async def my_coroutine():
    while True:
        result = await prompt_async('Say something: ', patch_stdout=True)
        print('You said: %s' % result)
</code></pre>

<p>I have managed to make this work from the asyncio event loop:</p>

<pre><code>import asyncio
l = asyncio.get_event_loop()
l.create_task(my_coroutine())
l.run_forever()

Say something: Hello
You said: Hello
</code></pre>

<p>However, I have failed to make it work from the tornado event loop. I have tried the following:</p>

<pre><code>from tornado.ioloop import IOLoop
IOLoop.current().run_sync(my_coroutine)
</code></pre>

<p>This will issue the initial prompt but then appears to block the console.</p>

<p>I have also tried:</p>

<pre><code>IOLoop.current().add_callback(my_coroutine)
IOLoop.current().start()
</code></pre>

<p>This does the same thing, but also produces the error message:</p>

<pre><code>RuntimeWarning: coroutine 'my_coroutine' was never awaited
</code></pre>

<p>And I have tried:</p>

<pre><code>IOLoop.current().spawn_callback(my_coroutine)
IOLoop.current().start()
</code></pre>

<p>I am clearly not understanding something here.</p>

<p>Can anyone throw any light on how this should be done?</p>

<p>I am using: Python 3.5.0, tornado 4.3.</p>

<p>To use <a href="http://www.tornadoweb.org/en/stable/asyncio.html" rel="nofollow">Tornado's asyncio integration</a>, you must tell Tornado to use the asyncio event loop. Usually, that means doing this at the start of your app:</p>

<pre><code>from tornado.platform.asyncio import AsyncIOMainLoop
AsyncIOMainLoop().install()
</code></pre>

python tornado prompt toolkit