Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

python - aysncio cannot read stdin on Windows

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

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()

However, it raises an OSError: [WInError 100381] An operation was attempted on something that is not a socket.

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('
', '').replace('
', '')


@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()

And that raises a NotImplementedError in asyncio.base_events._make_read_pipe_transport

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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()

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...