monitoredqueuedevice.py revision 781d71db
1"""MonitoredQueue classes and functions."""
2
3# Copyright (C) PyZMQ Developers
4# Distributed under the terms of the Modified BSD License.
5
6
7from zmq import ZMQError, PUB
8from zmq.devices.proxydevice import ProxyBase, Proxy, ThreadProxy, ProcessProxy
9from zmq.devices.monitoredqueue import monitored_queue
10
11
12class MonitoredQueueBase(ProxyBase):
13    """Base class for overriding methods."""
14
15    _in_prefix = b''
16    _out_prefix = b''
17
18    def __init__(self, in_type, out_type, mon_type=PUB, in_prefix=b'in', out_prefix=b'out'):
19
20        ProxyBase.__init__(self, in_type=in_type, out_type=out_type, mon_type=mon_type)
21
22        self._in_prefix = in_prefix
23        self._out_prefix = out_prefix
24
25    def run_device(self):
26        ins,outs,mons = self._setup_sockets()
27        monitored_queue(ins, outs, mons, self._in_prefix, self._out_prefix)
28
29
30class MonitoredQueue(MonitoredQueueBase, Proxy):
31    """Class for running monitored_queue in the background.
32
33    See zmq.devices.Device for most of the spec. MonitoredQueue differs from Proxy,
34    only in that it adds a ``prefix`` to messages sent on the monitor socket,
35    with a different prefix for each direction.
36
37    MQ also supports ROUTER on both sides, which zmq.proxy does not.
38
39    If a message arrives on `in_sock`, it will be prefixed with `in_prefix` on the monitor socket.
40    If it arrives on out_sock, it will be prefixed with `out_prefix`.
41
42    A PUB socket is the most logical choice for the mon_socket, but it is not required.
43    """
44    pass
45
46
47class ThreadMonitoredQueue(MonitoredQueueBase, ThreadProxy):
48    """Run zmq.monitored_queue in a background thread.
49
50    See MonitoredQueue and Proxy for details.
51    """
52    pass
53
54
55class ProcessMonitoredQueue(MonitoredQueueBase, ProcessProxy):
56    """Run zmq.monitored_queue in a background thread.
57
58    See MonitoredQueue and Proxy for details.
59    """
60
61
62__all__ = [
63    'MonitoredQueue',
64    'ThreadMonitoredQueue',
65    'ProcessMonitoredQueue'
66]
67