Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 """KQueue-based IOLoop implementation for BSD/Mac systems."""
00017 from __future__ import absolute_import, division, print_function, with_statement
00018
00019 import select
00020
00021 from tornado.ioloop import IOLoop, PollIOLoop
00022
00023 assert hasattr(select, 'kqueue'), 'kqueue not supported'
00024
00025
00026 class _KQueue(object):
00027 """A kqueue-based event loop for BSD/Mac systems."""
00028 def __init__(self):
00029 self._kqueue = select.kqueue()
00030 self._active = {}
00031
00032 def fileno(self):
00033 return self._kqueue.fileno()
00034
00035 def close(self):
00036 self._kqueue.close()
00037
00038 def register(self, fd, events):
00039 if fd in self._active:
00040 raise IOError("fd %s already registered" % fd)
00041 self._control(fd, events, select.KQ_EV_ADD)
00042 self._active[fd] = events
00043
00044 def modify(self, fd, events):
00045 self.unregister(fd)
00046 self.register(fd, events)
00047
00048 def unregister(self, fd):
00049 events = self._active.pop(fd)
00050 self._control(fd, events, select.KQ_EV_DELETE)
00051
00052 def _control(self, fd, events, flags):
00053 kevents = []
00054 if events & IOLoop.WRITE:
00055 kevents.append(select.kevent(
00056 fd, filter=select.KQ_FILTER_WRITE, flags=flags))
00057 if events & IOLoop.READ or not kevents:
00058
00059 kevents.append(select.kevent(
00060 fd, filter=select.KQ_FILTER_READ, flags=flags))
00061
00062
00063 for kevent in kevents:
00064 self._kqueue.control([kevent], 0)
00065
00066 def poll(self, timeout):
00067 kevents = self._kqueue.control(None, 1000, timeout)
00068 events = {}
00069 for kevent in kevents:
00070 fd = kevent.ident
00071 if kevent.filter == select.KQ_FILTER_READ:
00072 events[fd] = events.get(fd, 0) | IOLoop.READ
00073 if kevent.filter == select.KQ_FILTER_WRITE:
00074 if kevent.flags & select.KQ_EV_EOF:
00075
00076
00077
00078
00079
00080
00081
00082 events[fd] = IOLoop.ERROR
00083 else:
00084 events[fd] = events.get(fd, 0) | IOLoop.WRITE
00085 if kevent.flags & select.KQ_EV_ERROR:
00086 events[fd] = events.get(fd, 0) | IOLoop.ERROR
00087 return events.items()
00088
00089
00090 class KQueueIOLoop(PollIOLoop):
00091 def initialize(self, **kwargs):
00092 super(KQueueIOLoop, self).initialize(impl=_KQueue(), **kwargs)