~ubuntu-branches/ubuntu/saucy/pyzmq/saucy-proposed

« back to all changes in this revision

Viewing changes to zmq/core/poll.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-05-12 13:59:20 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20130512135920-1d2md9w425iq3sb6
Tags: 13.1.0-1
* New upstream release built with zeromq3 (Closes: #698830)
  - drop all patches
* workaround-gevent.patch: workaround issue with gevent < 1.0
* noncopysend-test.patch: avoid uninitialized values in tests
* update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""0MQ polling related functions and classes."""
2
 
 
3
 
#
4
 
#    Copyright (c) 2010-2011 Brian E. Granger & Min Ragan-Kelley
5
 
#
6
 
#    This file is part of pyzmq.
7
 
#
8
 
#    pyzmq is free software; you can redistribute it and/or modify it under
9
 
#    the terms of the Lesser GNU General Public License as published by
10
 
#    the Free Software Foundation; either version 3 of the License, or
11
 
#    (at your option) any later version.
12
 
#
13
 
#    pyzmq is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    Lesser GNU General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the Lesser GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
 
22
 
#-----------------------------------------------------------------------------
23
 
# Imports
24
 
#-----------------------------------------------------------------------------
25
 
 
26
 
import zmq
27
 
from zmq.core._poll import _poll
28
 
from zmq.core.constants import POLLIN, POLLOUT, POLLERR
29
 
 
30
 
#-----------------------------------------------------------------------------
31
 
# Polling related methods
32
 
#-----------------------------------------------------------------------------
33
 
 
34
 
 
35
 
class Poller(object):
36
 
    """Poller()
37
 
 
38
 
    A stateful poll interface that mirrors Python's built-in poll.
39
 
    """
40
 
 
41
 
    def __init__(self):
42
 
        self.sockets = {}
43
 
 
44
 
    def register(self, socket, flags=POLLIN|POLLOUT):
45
 
        """p.register(socket, flags=POLLIN|POLLOUT)
46
 
 
47
 
        Register a 0MQ socket or native fd for I/O monitoring.
48
 
        
49
 
        register(s,0) is equivalent to unregister(s).
50
 
 
51
 
        Parameters
52
 
        ----------
53
 
        socket : zmq.Socket or native socket
54
 
            A zmq.Socket or any Python object having a ``fileno()`` 
55
 
            method that returns a valid file descriptor.
56
 
        flags : int
57
 
            The events to watch for.  Can be POLLIN, POLLOUT or POLLIN|POLLOUT.
58
 
            If `flags=0`, socket will be unregistered.
59
 
        """
60
 
        if flags:
61
 
            self.sockets[socket] = flags
62
 
        elif socket in self.sockets:
63
 
            # uregister sockets registered with no events
64
 
            self.unregister(socket)
65
 
        else:
66
 
            # ignore new sockets with no events
67
 
            pass
68
 
 
69
 
    def modify(self, socket, flags=POLLIN|POLLOUT):
70
 
        """p.modify(socket, flags=POLLIN|POLLOUT)
71
 
 
72
 
        Modify the flags for an already registered 0MQ socket or native fd.
73
 
        """
74
 
        self.register(socket, flags)
75
 
 
76
 
    def unregister(self, socket):
77
 
        """p.unregister(socket)
78
 
 
79
 
        Remove a 0MQ socket or native fd for I/O monitoring.
80
 
 
81
 
        Parameters
82
 
        ----------
83
 
        socket : Socket
84
 
            The socket instance to stop polling.
85
 
        """
86
 
        del self.sockets[socket]
87
 
 
88
 
    def poll(self, timeout=None):
89
 
        """p.poll(timeout=None)
90
 
 
91
 
        Poll the registered 0MQ or native fds for I/O.
92
 
 
93
 
        Parameters
94
 
        ----------
95
 
        timeout : float, int
96
 
            The timeout in milliseconds. If None, no `timeout` (infinite). This
97
 
            is in milliseconds to be compatible with ``select.poll()``. The
98
 
            underlying zmq_poll uses microseconds and we convert to that in
99
 
            this function.
100
 
        """
101
 
        if timeout is None:
102
 
            timeout = -1
103
 
        
104
 
        timeout = int(timeout)
105
 
        if timeout < 0:
106
 
            timeout = -1
107
 
        return _poll(list(self.sockets.items()), timeout=timeout)
108
 
 
109
 
 
110
 
def select(rlist, wlist, xlist, timeout=None):
111
 
    """select(rlist, wlist, xlist, timeout=None) -> (rlist, wlist, xlist)
112
 
 
113
 
    Return the result of poll as a lists of sockets ready for r/w/exception.
114
 
 
115
 
    This has the same interface as Python's built-in ``select.select()`` function.
116
 
 
117
 
    Parameters
118
 
    ----------
119
 
    timeout : float, int, optional
120
 
        The timeout in seconds. If None, no timeout (infinite). This is in seconds to be
121
 
        compatible with ``select.select()``. The underlying zmq_poll uses microseconds
122
 
        and we convert to that in this function.
123
 
    rlist : list of sockets/FDs
124
 
        sockets/FDs to be polled for read events
125
 
    wlist : list of sockets/FDs
126
 
        sockets/FDs to be polled for write events
127
 
    xlist : list of sockets/FDs
128
 
        sockets/FDs to be polled for error events
129
 
    
130
 
    Returns
131
 
    -------
132
 
    (rlist, wlist, xlist) : tuple of lists of sockets (length 3)
133
 
        Lists correspond to sockets available for read/write/error events respectively.
134
 
    """
135
 
    if timeout is None:
136
 
        timeout = -1
137
 
    # Convert from sec -> us for zmq_poll.
138
 
    # zmq_poll accepts 3.x style timeout in ms
139
 
    timeout = int(timeout*1000.0)
140
 
    if timeout < 0:
141
 
        timeout = -1
142
 
    sockets = []
143
 
    for s in set(rlist + wlist + xlist):
144
 
        flags = 0
145
 
        if s in rlist:
146
 
            flags |= POLLIN
147
 
        if s in wlist:
148
 
            flags |= POLLOUT
149
 
        if s in xlist:
150
 
            flags |= POLLERR
151
 
        sockets.append((s, flags))
152
 
    return_sockets = _poll(sockets, timeout)
153
 
    rlist, wlist, xlist = [], [], []
154
 
    for s, flags in return_sockets:
155
 
        if flags & POLLIN:
156
 
            rlist.append(s)
157
 
        if flags & POLLOUT:
158
 
            wlist.append(s)
159
 
        if flags & POLLERR:
160
 
            xlist.append(s)
161
 
    return rlist, wlist, xlist
162
 
 
163
 
#-----------------------------------------------------------------------------
164
 
# Symbols to export
165
 
#-----------------------------------------------------------------------------
166
 
 
167
 
__all__ = [ 'Poller', 'select' ]