Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 """Select-based IOLoop implementation.
00017
00018 Used as a fallback for systems that don't support epoll or kqueue.
00019 """
00020 from __future__ import absolute_import, division, print_function, with_statement
00021
00022 import select
00023
00024 from tornado.ioloop import IOLoop, PollIOLoop
00025
00026
00027 class _Select(object):
00028 """A simple, select()-based IOLoop implementation for non-Linux systems"""
00029 def __init__(self):
00030 self.read_fds = set()
00031 self.write_fds = set()
00032 self.error_fds = set()
00033 self.fd_sets = (self.read_fds, self.write_fds, self.error_fds)
00034
00035 def close(self):
00036 pass
00037
00038 def register(self, fd, events):
00039 if fd in self.read_fds or fd in self.write_fds or fd in self.error_fds:
00040 raise IOError("fd %s already registered" % fd)
00041 if events & IOLoop.READ:
00042 self.read_fds.add(fd)
00043 if events & IOLoop.WRITE:
00044 self.write_fds.add(fd)
00045 if events & IOLoop.ERROR:
00046 self.error_fds.add(fd)
00047
00048
00049
00050 self.read_fds.add(fd)
00051
00052 def modify(self, fd, events):
00053 self.unregister(fd)
00054 self.register(fd, events)
00055
00056 def unregister(self, fd):
00057 self.read_fds.discard(fd)
00058 self.write_fds.discard(fd)
00059 self.error_fds.discard(fd)
00060
00061 def poll(self, timeout):
00062 readable, writeable, errors = select.select(
00063 self.read_fds, self.write_fds, self.error_fds, timeout)
00064 events = {}
00065 for fd in readable:
00066 events[fd] = events.get(fd, 0) | IOLoop.READ
00067 for fd in writeable:
00068 events[fd] = events.get(fd, 0) | IOLoop.WRITE
00069 for fd in errors:
00070 events[fd] = events.get(fd, 0) | IOLoop.ERROR
00071 return events.items()
00072
00073
00074 class SelectIOLoop(PollIOLoop):
00075 def initialize(self, **kwargs):
00076 super(SelectIOLoop, self).initialize(impl=_Select(), **kwargs)