更新时间:2023-11-17 23:22:34
的 NotImplementedError
引发异常,因为的连接管不是由 SelectorEventLoop
,其中支持协同程序是默认的事件循环集ASYNCIO
。您需要使用 ProactorEventLoop
来支持Windows管道。然而,这仍然无法工作,因为显然 connect_read_pipe
和 connect_write_pipe
功能,不支持标准输入
/ 标准输出
/ 标准错误
或在Windows文件作为的Python 3.5.1。
The NotImplementedError
exception is raised because the connect pipes coroutines are not supported by the SelectorEventLoop
, which is the default event loop set on asyncio
. You need to use a ProactorEventLoop
to support pipes on Windows. However, it would still not work because apparently the connect_read_pipe
and connect_write_pipe
functions doesn't support stdin
/stdout
/stderr
or files in Windows as Python 3.5.1.
从标准输入
与异步行为是使用与循环的 run_in_executor
方法的线程读取的方法之一。下面是引用一个简单的例子:
One way to read from stdin
with an asynchronous behavior is using a thread with the loop's run_in_executor
method. Here is a simple example for reference:
import asyncio
import sys
async 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
方法。该线程保持阻塞,直到标准输入
接收到换行,在平均时间循环可***执行其他协同程序,如果他们存在。
In the example the function sys.stdin.readline()
is called within another thread by the loop.run_in_executor
method. The thread remains blocked until stdin
receives a linefeed, in the mean time the loop is free to execute others coroutines if they existed.