<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> import errno <EOL> from select import error as SelectError , poll <EOL> from select import POLLIN , POLLOUT , POLLHUP , POLLERR , POLLNVAL <EOL> from zope . interface import implementer <EOL> from twisted . python import log <EOL> from twisted . internet import posixbase <EOL> from twisted . internet . interfaces import IReactorFDSet <EOL> @ implementer ( IReactorFDSet ) <EOL> class PollReactor ( posixbase . PosixReactorBase , posixbase . _PollLikeMixin ) : <EOL> """<STR_LIT>""" <EOL> _POLL_DISCONNECTED = ( POLLHUP | POLLERR | POLLNVAL ) <EOL> _POLL_IN = POLLIN <EOL> _POLL_OUT = POLLOUT <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _poller = poll ( ) <EOL> self . _selectables = { } <EOL> self . _reads = { } <EOL> self . _writes = { } <EOL> posixbase . PosixReactorBase . __init__ ( self ) <EOL> def _updateRegistration ( self , fd ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _poller . unregister ( fd ) <EOL> except KeyError : <EOL> pass <EOL> mask = <NUM_LIT:0> <EOL> if fd in self . _reads : <EOL> mask = mask | POLLIN <EOL> if fd in self . _writes : <EOL> mask = mask | POLLOUT <EOL> if mask != <NUM_LIT:0> : <EOL> self . _poller . register ( fd , mask ) <EOL> else : <EOL> if fd in self . _selectables : <EOL> del self . _selectables [ fd ] <EOL> def _dictRemove ( self , selectable , mdict ) : <EOL> try : <EOL> fd = selectable . fileno ( ) <EOL> mdict [ fd ] <EOL> except : <EOL> for fd , fdes in self . _selectables . items ( ) : <EOL> if selectable is fdes : <EOL> break <EOL> else : <EOL> return <EOL> if fd in mdict : <EOL> del mdict [ fd ] <EOL> self . _updateRegistration ( fd ) <EOL> def addReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> fd = reader . fileno ( ) <EOL> if fd not in self . _reads : <EOL> self . _selectables [ fd ] = reader <EOL> self . _reads [ fd ] = <NUM_LIT:1> <EOL> self . _updateRegistration ( fd ) <EOL> def addWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> fd = writer . fileno ( ) <EOL> if fd not in self . _writes : <EOL> self . _selectables [ fd ] = writer <EOL> self . _writes [ fd ] = <NUM_LIT:1> <EOL> self . _updateRegistration ( fd ) <EOL> def removeReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> return self . _dictRemove ( reader , self . _reads ) <EOL> def removeWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> return self . _dictRemove ( writer , self . _writes ) <EOL> def removeAll ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _removeAll ( <EOL> [ self . _selectables [ fd ] for fd in self . _reads ] , <EOL> [ self . _selectables [ fd ] for fd in self . _writes ] ) <EOL> def doPoll ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> timeout = int ( timeout * <NUM_LIT:1000> ) <EOL> try : <EOL> l = self . _poller . poll ( timeout ) <EOL> except SelectError as e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . EINTR : <EOL> return <EOL> else : <EOL> raise <EOL> _drdw = self . _doReadOrWrite <EOL> for fd , event in l : <EOL> try : <EOL> selectable = self . _selectables [ fd ] <EOL> except KeyError : <EOL> continue <EOL> log . callWithLogger ( selectable , _drdw , selectable , fd , event ) <EOL> doIteration = doPoll <EOL> def getReaders ( self ) : <EOL> return [ self . _selectables [ fd ] for fd in self . _reads ] <EOL> def getWriters ( self ) : <EOL> return [ self . _selectables [ fd ] for fd in self . _writes ] <EOL> def install ( ) : <EOL> """<STR_LIT>""" <EOL> p = PollReactor ( ) <EOL> from twisted . internet . main import installReactor <EOL> installReactor ( p ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] </s>
