select.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2012 Facebook
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00006 # not use this file except in compliance with the License. You may obtain
00007 # a copy of the License at
00008 #
00009 #     http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00014 # License for the specific language governing permissions and limitations
00015 # under the License.
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             # Closed connections are reported as errors by epoll and kqueue,
00048             # but as zero-byte reads by select, so when errors are requested
00049             # we need to listen for both read and error.
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)


rosbridge_tools
Author(s): Jonathan Mace
autogenerated on Sat Dec 27 2014 11:25:59