add event names

This commit is contained in:
clowwindy 2014-06-01 16:21:33 +08:00
parent c721a1c02f
commit f49d086d6a
3 changed files with 60 additions and 27 deletions

View file

@ -32,7 +32,7 @@ from collections import defaultdict
__all__ = ['EventLoop', 'POLL_NULL', 'POLL_IN', 'POLL_OUT', 'POLL_ERR',
'POLL_HUP', 'POLL_NVAL']
'POLL_HUP', 'POLL_NVAL', 'EVENT_NAMES']
POLL_NULL = 0x00
POLL_IN = 0x01
@ -42,6 +42,16 @@ POLL_HUP = 0x10
POLL_NVAL = 0x20
EVENT_NAMES = {
POLL_NULL: 'POLL_NULL',
POLL_IN: 'POLL_IN',
POLL_OUT: 'POLL_OUT',
POLL_ERR: 'POLL_ERR',
POLL_HUP: 'POLL_HUP',
POLL_NVAL: 'POLL_NVAL',
}
class EpollLoop(object):
def __init__(self):
@ -144,15 +154,22 @@ class EventLoop(object):
def __init__(self):
if hasattr(select, 'epoll'):
self._impl = EpollLoop()
self._model = 'epoll'
elif hasattr(select, 'kqueue'):
self._impl = KqueueLoop()
self._model = 'kqueue'
elif hasattr(select, 'select'):
self._impl = SelectLoop()
self._model = 'select'
else:
raise Exception('can not find any available functions in select '
'package')
self._fd_to_f = {}
@property
def model(self):
return self._model
def poll(self, timeout=None):
events = self._impl.poll(timeout)
return ((self._fd_to_f[fd], event) for fd, event in events)