aysncio无法在Windows上阅读stdin

aysncio无法在Windows上阅读stdin,第1张

aysncio无法在Windows上阅读stdin

NotImplementedError
引发异常,因为连接管协程不被支持
SelectorEventLoop
,这是默认的事件循环
asyncio
。您需要使用
ProactorEventLoop
Windows支持Windows上的管道。但是,它仍然不起作用,因为在Windows中,
connect_read_pipe
and
connect_write_pipe
函数显然不支持Python
3.5.1中的
stdin
/
stdout
/
stderr
或文件。

一种

stdin
使用异步行为进行读取的
run_in_executor
方法是将线程与循环的方法一起使用。这是一个简单的示例供参考:

import asyncioimport sysasync def aio_readline(loop):    while True:        line = await loop.run_in_executor(None, sys.stdin.readline)        print('Got line:', line, end='')loop = asyncio.get_event_loop()loop.run_until_complete(aio_readline(loop))loop.close()

在示例中

sys.stdin.readline()
,该
loop.run_in_executor
方法在另一个线程内调用该函数。线程将保持阻塞状态,直到
stdin
收到换行符为止,与此同时,循环可以自由执行其他协程(如果存在)。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5645267.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存