monitoredqueuedevice.py 1.9 KB

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