Ticket #1918: kqreactor_patch.diff

File kqreactor_patch.diff, 4.0 KB (added by psykidellic, 5 years ago)

Patch for kqreactor using select26 module.

  • kqreactor.py

    old new  
    6565 
    6666from zope.interface import implements 
    6767 
    68 from kqsyscall import EVFILT_READ, EVFILT_WRITE, EV_DELETE, EV_ADD 
    69 from kqsyscall import kqueue, kevent 
     68try: 
     69    import select26 as select 
     70except ImportError: 
     71    import select 
     72 
     73# from kqsyscall import EVFILT_READ, EVFILT_WRITE, EV_DELETE, EV_ADD 
     74# from kqsyscall import kqueue, kevent 
    7075 
    7176from twisted.internet.interfaces import IReactorFDSet 
    7277 
     
    105110        Initialize kqueue object, file descriptor tracking dictionaries, and the 
    106111        base class. 
    107112        """ 
    108         self._kq = kqueue() 
     113        self._kq = select.kqueue() 
    109114        self._reads = {} 
    110115        self._writes = {} 
    111116        self._selectables = {} 
     
    113118 
    114119 
    115120    def _updateRegistration(self, *args): 
    116         self._kq.kevent([kevent(*args)], 0, 0) 
     121        self._kq.control([select.kevent(*args)], 0, 0) 
    117122 
    118123    def addReader(self, reader): 
    119124        """Add a FileDescriptor for notification of data available to read. 
     
    122127        if fd not in self._reads: 
    123128            self._selectables[fd] = reader 
    124129            self._reads[fd] = 1 
    125             self._updateRegistration(fd, EVFILT_READ, EV_ADD) 
     130            self._updateRegistration(fd, select.KQ_FILTER_READ, select.KQ_EV_ADD) 
    126131 
    127132    def addWriter(self, writer): 
    128133        """Add a FileDescriptor for notification of data available to write. 
     
    131136        if fd not in self._writes: 
    132137            self._selectables[fd] = writer 
    133138            self._writes[fd] = 1 
    134             self._updateRegistration(fd, EVFILT_WRITE, EV_ADD) 
     139            self._updateRegistration(fd, select.KQ_FILTER_WRITE, select.KQ_EV_ADD) 
    135140 
    136141    def removeReader(self, reader): 
    137142        """Remove a Selectable for notification of data available to read. 
     
    141146            del self._reads[fd] 
    142147            if fd not in self._writes: 
    143148                del self._selectables[fd] 
    144             self._updateRegistration(fd, EVFILT_READ, EV_DELETE) 
     149            self._updateRegistration(fd, select.KQ_FILTER_READ, select.KQ_EV_DELETE) 
    145150 
    146151    def removeWriter(self, writer): 
    147152        """Remove a Selectable for notification of data available to write. 
     
    151156            del self._writes[fd] 
    152157            if fd not in self._reads: 
    153158                del self._selectables[fd] 
    154             self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE) 
     159            self._updateRegistration(fd, select.KQ_FILTER_WRITE, select.KQ_EV_DELETE) 
    155160 
    156161    def removeAll(self): 
    157162        """Remove all selectables, and return a list of them.""" 
     
    159164            self.removeReader(self.waker) 
    160165        result = self._selectables.values() 
    161166        for fd in self._reads.keys(): 
    162             self._updateRegistration(fd, EVFILT_READ, EV_DELETE) 
     167            self._updateRegistration(fd, select.KQ_FILTER_READ, select.KQ_EV_DELETE) 
    163168        for fd in self._writes.keys(): 
    164             self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE) 
     169            self._updateRegistration(fd, select.KQ_FILTER_WRITE, select.KQ_EV_DELETE) 
    165170        self._reads.clear() 
    166171        self._writes.clear() 
    167172        self._selectables.clear() 
     
    186191            timeout = int(timeout * 1000) # convert seconds to milliseconds 
    187192 
    188193        try: 
    189             l = self._kq.kevent([], len(self._selectables), timeout) 
     194            l = self._kq.control([], len(self._selectables), timeout) 
    190195        except OSError, e: 
    191196            if e[0] == errno.EINTR: 
    192197                return 
     
    206211 
    207212    def _doWriteOrRead(self, selectable, fd, filter): 
    208213        try: 
    209             if filter == EVFILT_READ: 
     214            if filter == select.KQ_FILTER_READ: 
    210215                why = selectable.doRead() 
    211             if filter == EVFILT_WRITE: 
     216            if filter == select.KQ_FILTER_WRITE: 
    212217                why = selectable.doWrite() 
    213218            if not selectable.fileno() == fd: 
    214219                why = main.CONNECTION_LOST