2016-09-30 48 views
0

我想从使用tornado事件循环的应用程序中使用prompt_toolkit,但我无法找到将prompt_toolkit提示符添加到事件循环的正确方法。如何从龙卷风事件循环调用prompt_toolkit?

的prompt_toolkit文档在ASYNCIO使用它(Asyncio Docs)的例子:

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) 

我已成功地使来自ASYNCIO事件循环这项工作:

import asyncio 
l = asyncio.get_event_loop() 
l.create_task(my_coroutine()) 
l.run_forever() 

Say something: Hello 
You said: Hello 

不过,我有未能通过龙卷风事件循环使其工作。我曾尝试以下:

from tornado.ioloop import IOLoop 
IOLoop.current().run_sync(my_coroutine) 

这将发出最初的提示,但随后出现阻止控制台。

我也曾尝试:

IOLoop.current().add_callback(my_coroutine) 
IOLoop.current().start() 

这做同样的事情,但也产生错误信息:

RuntimeWarning: coroutine 'my_coroutine' was never awaited 

我曾尝试:

IOLoop.current().spawn_callback(my_coroutine) 
IOLoop.current().start() 

我显然这里没有理解的东西。

任何人都可以告诉我们应该怎么做?

我正在使用:Python 3.5.0,龙卷风4.3。

回答

0

要使用Tornado's asyncio integration,您必须告诉Tornado使用asyncio事件循环。通常,这意味着在您的应用程序开始时执行此操作:

from tornado.platform.asyncio import AsyncIOMainLoop 
AsyncIOMainLoop().install()