Beispiel #1
0
 def run(self):
     while True:
         # Process the queue to be thread-safe =P
         while True:
             try:
                 mode, sock, sock_mode = self.__queue_sockets__.get_nowait()
             except Queue.Empty:
                 break
             if mode == EPollActions.ADD:
                 self.__manager__.register(sock.fileno(), sock_mode)
                 self.__registered_sockets__[sock.fileno()] = sock 
             elif mode == EPollActions.MODIFY:
                 self.__manager__.modify(sock.fileno(), sock_mode)
             elif mode == EPollActions.REMOVE:
                 self.__manager__.unregister(sock.fileno())
                 self.__registered_sockets__.pop(sock.fileno(), None)
         socks = self.__registered_sockets__
         write_sockets = []
         read_sockets = []
         for sockno, event in self.__manager__.poll(self.__timeout__):
             sock = socks[sockno]  # If it raises a error, it's a error in the logic..
             if event & select.EPOLLIN:
                 if isAsync(sock):
                     try:
                         sock.handle_read_event()  # If server it call handle_accept, if client it call handle_read..
                     except:
                         pass  # Just catch the error
                 else:
                     read_sockets.append(sock)
             elif event & select.EPOLLOUT:
                 if isAsync(sock):
                     try:
                         sock.handle_write()
                     except:
                         pass  # Just catch the error
                 else:
                     write_sockets.append(sock)
             elif event & select.EPOLLHUP:
                 if isAsync(sock):
                     try:
                         sock.handle_close()
                     except:
                         pass  # Just catch the error
                 else:
                     sock.close()
                 self.remove(sock)
         self.__new_modify_sockets__.acquire()  # Block until all events is set
         map(threading.Event.set, self.__new_sockets__)  # Set all flags that are waiting to True..
         self.__new_modify_sockets__.release()  # Release..
Beispiel #2
0
 def run(self):
     '''Run the thread listener in a separate thread, and send the data to other channels (if not sync)'''
     while True:
         # Runs infinitely
         while True:
             try:
                 action, sock, mode = self.__actions__.get_nowait()  # Get a item on the queue
             except Queue.Empty:
                 break  # Or break if empty =P
             if action == SelectActions.ADD:
                 self.__modes__[mode].append(sock)
             elif action == SelectActions.REMOVE and sock in self.__modes__[mode]:
                 self.__modes__[mode].remove(sock)
         if not self.__read_sockets__ and not self.__write_sockets__:
             time.sleep([self.__timeout__, 1][self.__timeout__ == None])  # Sleep the thread
             continue  # Continue and repeat
         read_sockets = getSockets(self.__read_sockets__)  # Organize it..xD
         write_sockets = getSockets(self.__write_sockets__)  # Organize it..xD
         to_read, to_write, to_except = select.select(read_sockets.keys(), write_sockets.keys(), read_sockets.keys() + write_sockets.keys(), self.__timeout__)
         read_events = []
         write_events = []
         for sockno in to_read:
             # If socket is async...we have to see if it's a client, if it is, we call handle_read, if it's a server, we call handle_accept, otherwise, we publish it in a list
             sock = read_sockets[sockno]
             if isAsync(sock):  # If async, call the methods..
                 sock.handle_read_event()
             else:
                 read_events.append(sock)
         for sockno in to_write:
             # If socket is async..we call the handler to write to the socket..If not, we publish it in a list..=P
             sock = write_sockets[sockno]
             if isAsync(sock):
                 sock.handle_write()
             else:
                 write_events.append(sock)
         for sockno in to_except:
             # Closes all sockets that emit a exceptional condition
             sock = read_sockets.get(sockno, write_sockets.get(sockno))
             sock.close()  # If not exist, we have a big error in question of:::: Logic =P
         self.__read_events__ = read_events  # Just replace the list of sockets that can be readed (PS: It's thread-safe)
         self.__write_events__ = write_events  # Just replace the list of sockets that can be writed (PS: It's thread-safe)