6
$ 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'
tshepang
  • 12,111
  • 21
  • 91
  • 136
Zhang Jiuzhou
  • 759
  • 8
  • 22

3 Answers3

7

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?

synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
1

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.

1

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
Luke Exton
  • 3,506
  • 2
  • 19
  • 33
  • 1
    My team has run into this problem and I've never figured out what distinguishes the two versions - but your post gave me an idea. I've been able to verify this theory on several OS X machines. If the Python distribution says it was compile with GCC compatible clang, it does not seem to have select.poll. If it is compiled with GCC itself, it does. – Rob Oct 21 '16 at 16:19
  • 1
    So the actual python complier seems to make a difference. Its missing if it is compiled with clang rather than gcc. – Luke Exton Oct 21 '16 at 16:40