Permalink
Browse files

Moved GreenPoller to separate poll.py

  • Loading branch information...
1 parent 059a08a commit 2699aa176ad8c4bc75100b77c21f6381fd7c3782 @tmc committed Jul 1, 2012
Showing with 87 additions and 160 deletions.
  1. +4 −3 gevent_zeromq/__init__.py
  2. +0 −78 gevent_zeromq/core.py
  3. +0 −79 gevent_zeromq/core.pyx
  4. +83 −0 gevent_zeromq/poll.py
@@ -22,13 +22,14 @@
from zmq import *
from zmq import devices
import gevent_zeromq.core as zmq
+from gevent_zeromq.poll import GreenPoller
zmq.Socket = zmq.GreenSocket
zmq.Context = zmq.GreenContext
-zmq.Poller = zmq.GreenPoller
+zmq.Poller = GreenPoller
Socket = zmq.GreenSocket
Context = zmq.GreenContext
-Poller = zmq.GreenPoller
+Poller = GreenPoller
def monkey_patch():
"""
@@ -40,6 +41,6 @@ def monkey_patch():
ozmq = __import__('zmq')
ozmq.Socket = zmq.GreenSocket
ozmq.Context = zmq.GreenContext
- ozmq.Poller = zmq.GreenPoller
+ ozmq.Poller = GreenPoller
__all__ = zmq.__all__ + ['monkey_patch']
View
@@ -173,81 +173,3 @@ class GreenContext(Context):
"""
_socket_class = GreenSocket
-
-class GreenPoller(Poller):
- """Replacement for :class:`zmq.core.Poller`
-
- Ensures that the greened Poller below is used in calls to
- :meth:`zmq.core.Poller.poll`.
- """
-
- def _get_descriptors(self):
- """Returns three elements tuple with socket descriptors ready
- for gevent.select.select
- """
- rlist = []
- wlist = []
- xlist = []
-
- for socket, flags in self.sockets.items():
- if isinstance(socket, zmq.Socket):
- rlist.append(socket.getsockopt(zmq.FD))
- continue
- elif isinstance(socket, int):
- fd = socket
- elif hasattr(socket, 'fileno'):
- try:
- fd = int(socket.fileno())
- except:
- raise ValueError('fileno() must return an valid integer fd')
- else:
- raise TypeError('Socket must be a 0MQ socket, an integer fd '
- 'or have a fileno() method: %r' % socket)
-
- if flags & zmq.POLLIN:
- rlist.append(fd)
- if flags & zmq.POLLOUT:
- wlist.append(fd)
- if flags & zmq.POLLERR:
- xlist.append(fd)
-
- return (rlist, wlist, xlist)
-
- def poll(self, timeout=-1):
- """Overridden method to ensure that the green version of
- Poller is used.
-
- Behaves the same as :meth:`zmq.core.Poller.poll`
- """
-
- if timeout is None:
- timeout = -1
-
- if timeout < 0:
- timeout = -1
-
- rlist = None
- wlist = None
- xlist = None
-
- if timeout > 0:
- tout = gevent.Timeout.start_new(timeout/1000.0)
-
- try:
- # Loop until timeout or events available
- rlist, wlist, xlist = self._get_descriptors()
- while True:
- events = super(GreenPoller, self).poll(0)
- if events or timeout == 0:
- return events
-
- # wait for activity on sockets in a green way
- select.select(rlist, wlist, xlist)
-
- except gevent.Timeout, t:
- if t is not tout:
- raise
- return []
- finally:
- if timeout > 0:
- tout.cancel()
View
@@ -163,82 +163,3 @@ class GreenContext(_Context):
Ensures that the greened Socket below is used in calls to `socket`.
"""
_socket_class = GreenSocket
-
-
-class GreenPoller(Poller):
- """Replacement for :class:`zmq.core.Poller`
-
- Ensures that the greened Poller below is used in calls to
- :meth:`zmq.core.Poller.poll`.
- """
-
- def _get_descriptors(self):
- """Returns three elements tuple with socket descriptors ready
- for gevent.select.select
- """
- rlist = []
- wlist = []
- xlist = []
-
- for socket, flags in self.sockets.items():
- if isinstance(socket, zmq.Socket):
- rlist.append(socket.getsockopt(zmq.FD))
- continue
- elif isinstance(socket, int):
- fd = socket
- elif hasattr(socket, 'fileno'):
- try:
- fd = int(socket.fileno())
- except:
- raise ValueError('fileno() must return an valid integer fd')
- else:
- raise TypeError('Socket must be a 0MQ socket, an integer fd '
- 'or have a fileno() method: %r' % socket)
-
- if flags & zmq.POLLIN:
- rlist.append(fd)
- if flags & zmq.POLLOUT:
- wlist.append(fd)
- if flags & zmq.POLLERR:
- xlist.append(fd)
-
- return (rlist, wlist, xlist)
-
- def poll(self, timeout=-1):
- """Overridden method to ensure that the green version of
- Poller is used.
-
- Behaves the same as :meth:`zmq.core.Poller.poll`
- """
-
- if timeout is None:
- timeout = -1
-
- if timeout < 0:
- timeout = -1
-
- rlist = None
- wlist = None
- xlist = None
-
- if timeout > 0:
- tout = gevent.Timeout.start_new(timeout/1000.0)
-
- try:
- # Loop until timeout or events available
- rlist, wlist, xlist = self._get_descriptors()
- while True:
- events = super(GreenPoller, self).poll(0)
- if events or timeout == 0:
- return events
-
- # wait for activity on sockets in a green way
- select.select(rlist, wlist, xlist)
-
- except gevent.Timeout, t:
- if t is not tout:
- raise
- return []
- finally:
- if timeout > 0:
- tout.cancel()
View
@@ -0,0 +1,83 @@
+import zmq
+from zmq import Poller
+import gevent
+from gevent import select
+
+
+class GreenPoller(Poller):
+ """Replacement for :class:`zmq.core.Poller`
+
+ Ensures that the greened Poller below is used in calls to
+ :meth:`zmq.core.Poller.poll`.
+ """
+
+ def _get_descriptors(self):
+ """Returns three elements tuple with socket descriptors ready
+ for gevent.select.select
+ """
+ rlist = []
+ wlist = []
+ xlist = []
+
+ for socket, flags in self.sockets.items():
+ if isinstance(socket, zmq.Socket):
+ rlist.append(socket.getsockopt(zmq.FD))
+ continue
+ elif isinstance(socket, int):
+ fd = socket
+ elif hasattr(socket, 'fileno'):
+ try:
+ fd = int(socket.fileno())
+ except:
+ raise ValueError('fileno() must return an valid integer fd')
+ else:
+ raise TypeError('Socket must be a 0MQ socket, an integer fd '
+ 'or have a fileno() method: %r' % socket)
+
+ if flags & zmq.POLLIN:
+ rlist.append(fd)
+ if flags & zmq.POLLOUT:
+ wlist.append(fd)
+ if flags & zmq.POLLERR:
+ xlist.append(fd)
+
+ return (rlist, wlist, xlist)
+
+ def poll(self, timeout=-1):
+ """Overridden method to ensure that the green version of
+ Poller is used.
+
+ Behaves the same as :meth:`zmq.core.Poller.poll`
+ """
+
+ if timeout is None:
+ timeout = -1
+
+ if timeout < 0:
+ timeout = -1
+
+ rlist = None
+ wlist = None
+ xlist = None
+
+ if timeout > 0:
+ tout = gevent.Timeout.start_new(timeout/1000.0)
+
+ try:
+ # Loop until timeout or events available
+ rlist, wlist, xlist = self._get_descriptors()
+ while True:
+ events = super(GreenPoller, self).poll(0)
+ if events or timeout == 0:
+ return events
+
+ # wait for activity on sockets in a green way
+ select.select(rlist, wlist, xlist)
+
+ except gevent.Timeout, t:
+ if t is not tout:
+ raise
+ return []
+ finally:
+ if timeout > 0:
+ tout.cancel()

0 comments on commit 2699aa1

Please sign in to comment.