相关疑难解决方法(0)

aysncio无法在Windows上读取stdin

我试图在Windows 7 64位和Python 3.4.3上异步读取stdin

我尝试了这个受到SO回答的启发:

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()
Run Code Online (Sandbox Code Playgroud)

然而,它提出了一个OSError: [WInError 100381] An operation was attempted on something that is not a socket.

类似文件的对象是否stdin可以包含在类中以赋予它套接字的API?我已单独提出这个问题,但如果解决方案很简单,请在此处回答.

假设我无法包装类似文件的对象使其成为套接字,我尝试使用流作为这个要点的启发:

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 …
Run Code Online (Sandbox Code Playgroud)

python windows stdin asynchronous python-3.x

13
推荐指数
1
解决办法
2789
查看次数

为什么select.select()适用于磁盘文件但不适用于epoll()?

以下代码实际上是使用select.select()来获取文件:

f = open('node.py')
fd = f.fileno()
while True:
    r, w, e = select.select([fd], [], [])
    print '>', repr(os.read(fd, 10))
    time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

当我用epoll尝试类似的东西时,我收到一个错误:

self._impl.register(fd, events | self.ERROR)
IOError: [Errno 1] Operation not permitted 
Run Code Online (Sandbox Code Playgroud)

我还读过epoll不支持磁盘文件 - 或者说它没有意义.

epoll常规文件

但是为什么select()会支持磁盘文件呢?我查看了selectmodule.c中的实现,它似乎只是进入操作系统,即Python没有添加任何特殊支持.

在更高的层次上,我正在尝试在非阻塞服务器中提供静态文件的最佳方法.我想我会尝试创建从磁盘读取的I/O线程,并将数据提供给写入套接字的主事件循环线程.

python unix select epoll

7
推荐指数
1
解决办法
2857
查看次数

标签 统计

python ×2

asynchronous ×1

epoll ×1

python-3.x ×1

select ×1

stdin ×1

unix ×1

windows ×1