aysncio不能在Windows标准输入读取 [英] aysncio cannot read stdin on Windows

查看:402
本文介绍了aysncio不能在Windows标准输入读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows 7 64位和Python 3.4.3标准输入异步读

I'm trying to read stdin asynchronously on Windows 7 64-bit and Python 3.4.3

我试过这个灵感来自一<一href=\"http://stackoverflow.com/questions/25351999/what-file-descriptor-object-does-python-asyncios-loop-add-reader-expect#answer-25352042\">SO回答:

I tried this inspired by an SO answer:

import asyncio
import sys


def reader():
    print('Received:', sys.stdin.readline())


loop = asyncio.get_event_loop()
task = loop.add_reader(sys.stdin.fileno(), reader)
loop.run_forever()
loop.close()

但是,它提出了一个 OSERROR:[WInError 100381]一个操作尝试的东西,不是一个套接字

能像标准输入 A类文件对象中的一类包裹给它一个套接字的API?我有问到这个问题单独的,但如果解决办法很简单,请在这里回答

Could a file-like object like stdin be wrapped in a class to give it the API of a socket? I have asked this question separately, but if the solution is simple please answer here.

假设我不能换一个类文件对象,使其插座,我试图用流作为灵感这个要点

Assuming that I cannot wrap a file-like object to make it a socket, I tried using streams as inspired by this gist:

import asyncio
import sys


@asyncio.coroutine
def stdio(loop):
    reader = asyncio.StreamReader(loop=loop)
    reader_protocol = asyncio.StreamReaderProtocol(reader)
    yield from loop.connect_read_pipe(lambda: reader_protocol, sys.stdin)


@asyncio.coroutine
def async_input(loop):
    reader = yield from stdio(loop)
    line = yield from reader.readline()
    return line.decode().replace('\r', '').replace('\n', '')


@asyncio.coroutine
def main(loop):
    name = yield from async_input(loop)
    print('Hello ', name)


loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()

这提出了一个 NotImplementedError asyncio.base_events._make_read_pipe_transport

请指教如何读标准输入使用 ASYNCIO 在Windows ...

Please advise how to read stdin using asyncio on Windows...

推荐答案

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.

这篇关于aysncio不能在Windows标准输入读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆