8

I would like to use select.epoll() in my Python library.

Unfortunately epoll is not available everywhere.

I need a way to fallback to select.select().

I tried to find something at pypi, but failed to find a matching package: https://pypi.python.org/pypi?%3Aaction=search&term=epoll&submit=search

How could I solve this "fallback from epoll to select if epoll is not available"?

4
  • How about using libevent which wraps all these libevent.org Jan 11, 2018 at 9:20
  • 1
    @AkhilThayyil if you write your comment as answer, then I will upvote it. The libevent thing looks good.
    – guettli
    Jan 11, 2018 at 14:45
  • read before changing to epool: epoll is fundamentally broken part 1, epoll is fundamentally broken part 2
    – internety
    Jan 20, 2018 at 3:38
  • @internety thank you for the link to "epoll is fundamentally broken" at the current stage the concerns of this article don't apply to my use of epoll, but nevertheless good to know.
    – guettli
    Jan 22, 2018 at 8:03

4 Answers 4

10

Python 3.4 introduced the selectors module. It offers a DefaultSelector that is an alias to the "most efficient implementation available on the current platform".

Here's a quick usage example:

sel = selectors.DefaultSelector()

sel.register(fp1, selectors.EVENT_READ)
sel.register(fp2, selectors.EVENT_READ)
sel.register(fp3, selectors.EVENT_READ)

for key, mask in sel.select():
    print(key.fileobj)

You can find a more complete example on the Python documentation.

DefaultSelector will try, in this order:

  • epool (Linux), kqueue (FreeBSD / NetBSD / OpenBSD / OS X) or /dev/poll (Solaris)
  • poll (Unix)
  • select
4
+100

Aside from selectors stdlib, I would go with uvloop, which is built on top of libuv in Cython. Compare to libevent/libev, those two do not have actively maintained python binding, uvloop is more promise.

3

How about using libevent which wraps all polling mechanisms and falls back to the best available one based on your platform libevent.org

2
3

Here are my two cents on this. According to documentation, epoll is available only on Linux 2.5.44 and newer. With code:

import os
if os.uname()[0] != 'Linux' or os.uname()[2] < '2.5.44':
    #use select
else:
    #use epoll

Better yet, I reckon that the above could be turned into a nice decorator, to be used wherever you want in your program, that returns the correct function depending on the underlying os.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.