old_contents: __author__ = 'mazheng'
  
  import select
  import socket
  from threading import Thread
  
  
  class IOLoop(Thread):
  
      def __init__(self):
          super(IOLoop, self).__init__()
          self.select = select.epoll()
          self.connections = {}
          self.websockets = {}
  
      @staticmethod
      def instance():
          if not hasattr(IOLoop, "_instance"):
              IOLoop._instance = IOLoop()
          return IOLoop._instance
  
      def register(self, fileno, connection, websocket):
          self.select.register(fileno, select.EPOLLIN)
          self.connections[fileno] = connection
          self.websockets[fileno] = websocket
  
      def run(self):
          while True:
              epoll_list = self.select.poll(1)
              for fd, events in epoll_list:
                  print fd, events
                  if select.EPOLLIN & events:
                      error = False
                      while True:
                          try:
                              data = self.connections[fd].recv(1024)
                          except socket.error:
                              error = True
                          if not data or error:
                              break
                          self.websockets[fd].write_message(data)
                  elif select.EPOLLHUP & events:
                      self.close()
                  else:
                      continue
  
      def close(self, fd):
          self.select.unregister(fd)
          self.connections[fd].close()
          self.websockets[fd].close()
          del self.connections[fd]
          del self.websockets[fd]
  
new_contents: __author__ = 'mazheng'
  
  import select
  import socket
  import errno
  from threading import Thread
  
  
  class IOLoop(Thread):
  
      def __init__(self):
          super(IOLoop, self).__init__()
          self.select = select.epoll()
          self.connections = {}
          self.websockets = {}
  
      @staticmethod
      def instance():
          if not hasattr(IOLoop, "_instance"):
              IOLoop._instance = IOLoop()
          return IOLoop._instance
  
      def register(self, fileno, connection, websocket):
          self.select.register(fileno, select.EPOLLIN | select.EPOLLET)
          self.connections[fileno] = connection
          self.websockets[fileno] = websocket
  
      def run(self):
          while True:
              epoll_list = self.select.poll()
              for fd, events in epoll_list:
                  if select.EPOLLIN & events:
                      while True:
                          try:
                              data = self.connections[fd].recv(1024)
                          except socket.error, e:
                              if e.errno == errno.EAGAIN:
                                  self.select.modify(fd, select.EPOLLET)
                              elif isinstance(e, socket.timeout):
                                  break
                              else:
                                  self.close(fd)
                          self.websockets[fd].write_message(data)
                  elif select.EPOLLHUP & events:
                      self.close(fd)
                  else:
                      continue
  
      def close(self, fd):
          self.select.unregister(fd)
          self.connections[fd].close()
          self.websockets[fd].close()
          del self.connections[fd]
          del self.websockets[fd]
  
subject: use et mode