if hasattr(select, 'kqueue'):
class KqueueSelector(_BaseSelectorImpl):
"""Kqueue-based selector."""
self._selector = select.kqueue()
return self._selector.fileno()
def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data)
kev = select.kevent(key.fd, select.KQ_FILTER_READ,
self._selector.control([kev], 0, 0)
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
self._selector.control([kev], 0, 0)
super().unregister(fileobj)
def unregister(self, fileobj):
key = super().unregister(fileobj)
if key.events & EVENT_READ:
kev = select.kevent(key.fd, select.KQ_FILTER_READ,
self._selector.control([kev], 0, 0)
# This can happen if the FD was closed since it
if key.events & EVENT_WRITE:
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
self._selector.control([kev], 0, 0)
def select(self, timeout=None):
timeout = None if timeout is None else max(timeout, 0)
max_ev = len(self._fd_to_key)
kev_list = self._selector.control(None, max_ev, timeout)
if flag == select.KQ_FILTER_READ:
if flag == select.KQ_FILTER_WRITE:
key = self._key_from_fd(fd)
ready.append((key, events & key.events))
# Choose the best implementation, roughly:
# epoll|kqueue|devpoll > poll > select.
# select() also can't accept a FD > FD_SETSIZE (usually around 1024)
if 'KqueueSelector' in globals():
DefaultSelector = KqueueSelector
elif 'EpollSelector' in globals():
DefaultSelector = EpollSelector
elif 'DevpollSelector' in globals():
DefaultSelector = DevpollSelector
elif 'PollSelector' in globals():
DefaultSelector = PollSelector
DefaultSelector = SelectSelector