用于扭曲的用户输入的异步prompt_toolkit

时间:2018-01-25 19:32:09

标签: python twisted prompt-toolkit

我正在尝试编写一个简单的命令行客户端作为TCP客户端/服务器系统的包装器。客户端使用twisted框架在python中编写。我使用Python3.6作为解释器。我发现prompt_toolkit有一个asyncio实现,并尝试使用示例代码将其连接到使用ensureDeferred的扭曲系统。不幸的是,一旦显示提示,代码就会抛出异常。

我是新手在python中使用asyncio库工具,所以我不确定我是否遗漏了一些东西。我试图运行的代码如下:

from twisted.internet import reactor
from twisted.internet.defer import ensureDeferred
from prompt_toolkit import prompt_async


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


d = ensureDeferred(my_coroutine())
reactor.run()

引发的异常如下:

 AssertionError: yield from wasn't used with future

我不确定这是扭曲和asyncio之间的基本不兼容,还是我错过了连接这两个系统的关键方法。我非常感谢任何建议。

版本细节:

twisted == 17.9.0
prompt_toolkit == 1.0.15

1 个答案:

答案 0 :(得分:0)

您要做的第一件事就是安装asyncioreactor,以便Twisted的本机事件循环与asyncio事件循环并行工作。

import twisted.internet.asyncioreactor
twisted.internet.asyncioreactor.install()
# add your imports + code here

我不确定asyncioreactor对于生产使用的准备程度如何,这可能是文档方面不多的原因。

资源