monitoredqueuedevice.py 1.9 KB

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