为什么select.select()处理磁盘文件而不处理epoll()? [英] Why does select.select() work with disk files but not epoll()?
问题描述
以下代码本质上是使用select.select()整理文件的:
The following code essentially cats a file with 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)
当我尝试使用epoll做类似的事情时,我得到一个错误:
When I try a similar thing with epoll I get an error:
self._impl.register(fd, events | self.ERROR)
IOError: [Errno 1] Operation not permitted
我还读到epoll不支持磁盘文件-也许没有意义.
I've also read that epoll does not support disk files -- or perhaps that it doesn't make sense.
但是为什么select()支持磁盘文件呢?我查看了selectmodule.c中的实现,它似乎只是进入了操作系统,即Python没有添加任何特殊支持.
But why does select() support disk files then? I looked at the implementation in selectmodule.c and it seems to just be going through to the operating system, i.e. Python is not adding any special support.
在更高层次上,我正在尝试在无阻塞服务器中提供静态文件的最佳方法.我想我将尝试创建从磁盘读取的I/O线程,并将数据提供给写入套接字的主事件循环线程.
On a higher level I'm experimenting with the best way to serve static files in a nonblocking server. I guess I will try creating I/O threads that read from disk and feed data to the main event loop thread that writes to sockets.
推荐答案
select允许监视指向常规文件的文件描述符,但是它始终将文件报告为可读/可写(即,它有点用处,因为它不会告诉您读/写是否实际上会阻塞).
select allows filedescriptors pointing to regular files to be monitored, however it will always report a file as readable/writable (i.e. it's somewhat useless, as it doesn't tell you whether a read/write would actually block).
epoll不允许监视常规文件,因为它没有(至少在Linux上)没有可用的机制来判断读/写常规文件是否会阻止
epoll just disallows monitoring of regular files, as it has no mechanism (on linux at least) available to tell whether reading/writing a regular file would block
这篇关于为什么select.select()处理磁盘文件而不处理epoll()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!