Cannot use Python select.poll in Mac OS?

我的梦境 提交于 2020-01-01 09:41:23

问题


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

回答1:


Instead of using poll, use select.kqueue() on OSX. It's similar to 'epoll' on Linux in that you can more efficiently register for types of file-descriptor / file system events which can be used in asynchronous code. Much more efficient than polling.

Otherwise, the equivalent is just running a blocking select.select() inside a while True: loop with some sort of timeout?




回答2:


If you want to use poll in order to not rewrite a bunch of code for kqueue, it is built in to the python compiled from macports (macports.org). You just must specify that python instance explicitly (/opt/local/bin/python2.7 in my case) because OSX's python (/usr/bin/python) will by default be earlier in the search path.




回答3:


Interestingly for future reference, this only comes up with a limited subset of the versions of 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


来源:https://stackoverflow.com/questions/19740471/cannot-use-python-select-poll-in-mac-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!