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

时间:2013-11-02 09:11:52

标签: python macos networking posix-select

$ 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'

3 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

有趣的是,为了将来参考,这只会出现一些有限的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