Commit 94b56117 authored by Eddi Linder's avatar Eddi Linder

Added support for a green poll (includes monkey patching)

parent 6fe727c0
......@@ -248,7 +248,6 @@ def patch_select(aggressive=True):
# since these are blocking we're removing them here. This makes some other
# modules (e.g. asyncore) non-blocking, as they use select that we provide
# when none of these are available.
remove_item(select, 'poll')
remove_item(select, 'epoll')
remove_item(select, 'kqueue')
remove_item(select, 'kevent')
......@@ -268,9 +267,8 @@ def patch_select(aggressive=True):
if aggressive:
# If `selectors` had already been imported before we removed
# select.poll|epoll|kqueue, these may have been defined in terms
# select.epoll|kqueue, these may have been defined in terms
# of those functions. They'll fail at runtime.
remove_item(selectors, 'PollSelector')
remove_item(selectors, 'EpollSelector')
remove_item(selectors, 'KqueueSelector')
selectors.DefaultSelector = selectors.SelectSelector
......
......@@ -3,11 +3,13 @@ from __future__ import absolute_import
from gevent.event import Event
from gevent.hub import get_hub
from gevent.hub import integer_types
from select import POLLIN, POLLOUT
__implements__ = ['select']
__implements__ = ['select', 'poll']
__all__ = ['error'] + __implements__
import select as __select__
error = __select__.error
......@@ -16,14 +18,13 @@ def get_fileno(obj):
fileno_f = obj.fileno
except AttributeError:
if not isinstance(obj, integer_types):
raise TypeError('argument must be an int, or have a fileno() method: %r' % (obj, ))
raise TypeError('argument must be an int, or have a fileno() method: %r' % (obj,))
return obj
else:
return fileno_f()
class SelectResult(object):
__slots__ = ['read', 'write', 'event']
def __init__(self):
......@@ -69,3 +70,49 @@ def select(rlist, wlist, xlist, timeout=None):
finally:
for watcher in watchers:
watcher.stop()
class PollResult(object):
__slots__ = ['events', 'event']
def __init__(self):
self.events = []
self.event = Event()
def add_event(self, events, fd):
result_flags = 0
result_flags |= POLLIN if events & 1 else 0
result_flags |= POLLOUT if events & 2 else 0
self.events.append((fd, result_flags))
self.event.set()
class poll(object):
def __init__(self):
self.fds = {}
self.loop = get_hub().loop
def register(self, fd, eventmask=POLLIN | POLLOUT):
flags = 0
flags |= 1 if eventmask & POLLIN else 0
flags |= 2 if eventmask & POLLOUT else 0
watcher = self.loop.io(get_fileno(fd), flags)
watcher.priority = self.loop.MAXPRI
self.fds[fd] = watcher
def modify(self, fd, eventmask):
self.register(fd, eventmask)
def poll(self, timeout=None):
result = PollResult()
try:
for fd in self.fds:
self.fds[fd].start(result.add_event, get_fileno(fd), pass_events=True)
result.event.wait(timeout=timeout)
return result.events
finally:
for fd in self.fds:
self.fds[fd].stop()
def unregister(self, fd):
self.fds.pop(fd, None)
......@@ -24,6 +24,19 @@ if sys.platform != 'win32':
os.close(w)
class TestPollRead(greentest.GenericWaitTestCase):
def wait(self, timeout):
r, w = os.pipe()
try:
poll = select.poll()
poll.register(r)
poll.poll(timeout)
poll.unregister(r)
finally:
os.close(r)
os.close(w)
class TestSelectTypes(greentest.TestCase):
def test_int(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment