无法在 Mac OS 中使用 Python select.poll


$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.poll
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'poll'

不要使用poll,而是在OSX上使用select.kqueue()。它类似于 Linux 上的"epoll",因为您可以更有效地注册可在异步代码中使用的文件描述符/文件系统事件类型。比轮询效率高得多。

否则,等效的只是在一段时间内运行阻塞 select.select() True: 循环与某种超时?

如果你想

使用poll来不为kqueue重写一堆代码,它内置于从macports编译的python中(macports.org)。你只需要明确指定那个python实例(在我的例子中是/opt/local/bin/python2.7),因为OSX的python(/usr/bin/python)默认在搜索路径中处于较早的位置。

有趣的是,为了将来参考,这只提出了python版本的有限子集

user@hostname:~/ws/engine$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.poll()
<select.poll object at 0x102415cc0>
>>> exit()
user@hostname:~/ws/engine$ python --version
Python 2.7.9
user@hostname:~/ws/engine$ workon py_2_7_10
(py_2_7_10) user@hostname:~/ws/engine$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.poll()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'poll'
>>> 
~/ws/engine$ uname -a
Darwin hostname 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64

最新更新