Frage

$ 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'
War es hilfreich?

Lösung

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?

Andere Tipps

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.

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top