1
"""0MQ polling related functions and classes."""
4
# Copyright (c) 2010-2011 Brian E. Granger & Min Ragan-Kelley
6
# This file is part of pyzmq.
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.
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.
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/>.
22
#-----------------------------------------------------------------------------
24
#-----------------------------------------------------------------------------
27
from zmq.core._poll import _poll
28
from zmq.core.constants import POLLIN, POLLOUT, POLLERR
30
#-----------------------------------------------------------------------------
31
# Polling related methods
32
#-----------------------------------------------------------------------------
38
A stateful poll interface that mirrors Python's built-in poll.
44
def register(self, socket, flags=POLLIN|POLLOUT):
45
"""p.register(socket, flags=POLLIN|POLLOUT)
47
Register a 0MQ socket or native fd for I/O monitoring.
49
register(s,0) is equivalent to unregister(s).
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.
57
The events to watch for. Can be POLLIN, POLLOUT or POLLIN|POLLOUT.
58
If `flags=0`, socket will be unregistered.
61
self.sockets[socket] = flags
62
elif socket in self.sockets:
63
# uregister sockets registered with no events
64
self.unregister(socket)
66
# ignore new sockets with no events
69
def modify(self, socket, flags=POLLIN|POLLOUT):
70
"""p.modify(socket, flags=POLLIN|POLLOUT)
72
Modify the flags for an already registered 0MQ socket or native fd.
74
self.register(socket, flags)
76
def unregister(self, socket):
77
"""p.unregister(socket)
79
Remove a 0MQ socket or native fd for I/O monitoring.
84
The socket instance to stop polling.
86
del self.sockets[socket]
88
def poll(self, timeout=None):
89
"""p.poll(timeout=None)
91
Poll the registered 0MQ or native fds for I/O.
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
104
timeout = int(timeout)
107
return _poll(list(self.sockets.items()), timeout=timeout)
110
def select(rlist, wlist, xlist, timeout=None):
111
"""select(rlist, wlist, xlist, timeout=None) -> (rlist, wlist, xlist)
113
Return the result of poll as a lists of sockets ready for r/w/exception.
115
This has the same interface as Python's built-in ``select.select()`` function.
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
132
(rlist, wlist, xlist) : tuple of lists of sockets (length 3)
133
Lists correspond to sockets available for read/write/error events respectively.
137
# Convert from sec -> us for zmq_poll.
138
# zmq_poll accepts 3.x style timeout in ms
139
timeout = int(timeout*1000.0)
143
for s in set(rlist + wlist + xlist):
151
sockets.append((s, flags))
152
return_sockets = _poll(sockets, timeout)
153
rlist, wlist, xlist = [], [], []
154
for s, flags in return_sockets:
161
return rlist, wlist, xlist
163
#-----------------------------------------------------------------------------
165
#-----------------------------------------------------------------------------
167
__all__ = [ 'Poller', 'select' ]