frame.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # coding: utf-8
  2. """0MQ Frame pure Python methods."""
  3. # Copyright (C) PyZMQ Developers
  4. # Distributed under the terms of the Modified BSD License.
  5. from .attrsettr import AttributeSetter
  6. from zmq.backend import Frame as FrameBase
  7. import zmq
  8. def _draft(v, feature):
  9. zmq.error._check_version(v, feature)
  10. if not zmq.DRAFT_API:
  11. raise RuntimeError("libzmq and pyzmq must be built with draft support for %s" % feature)
  12. class Frame(FrameBase, AttributeSetter):
  13. """Frame(data=None, track=False, copy=None, copy_threshold=zmq.COPY_THRESHOLD)
  14. A zmq message Frame class for non-copy send/recvs.
  15. This class is only needed if you want to do non-copying send and recvs.
  16. When you pass a string to this class, like ``Frame(s)``, the
  17. ref-count of `s` is increased by two: once because the Frame saves `s` as
  18. an instance attribute and another because a ZMQ message is created that
  19. points to the buffer of `s`. This second ref-count increase makes sure
  20. that `s` lives until all messages that use it have been sent. Once 0MQ
  21. sends all the messages and it doesn't need the buffer of s, 0MQ will call
  22. ``Py_DECREF(s)``.
  23. Parameters
  24. ----------
  25. data : object, optional
  26. any object that provides the buffer interface will be used to
  27. construct the 0MQ message data.
  28. track : bool [default: False]
  29. whether a MessageTracker_ should be created to track this object.
  30. Tracking a message has a cost at creation, because it creates a threadsafe
  31. Event object.
  32. copy : bool [default: use copy_threshold]
  33. Whether to create a copy of the data to pass to libzmq
  34. or share the memory with libzmq.
  35. If unspecified, copy_threshold is used.
  36. copy_threshold: int [default: zmq.COPY_THRESHOLD]
  37. If copy is unspecified, messages smaller than this many bytes
  38. will be copied and messages larger than this will be shared with libzmq.
  39. """
  40. def __getitem__(self, key):
  41. # map Frame['User-Id'] to Frame.get('User-Id')
  42. return self.get(key)
  43. @property
  44. def group(self):
  45. """The RADIO-DISH group of the message.
  46. Requires libzmq >= 4.2 and pyzmq built with draft APIs enabled.
  47. .. versionadded:: 17
  48. """
  49. _draft((4,2), "RADIO-DISH")
  50. return self.get('group')
  51. @group.setter
  52. def group(self, group):
  53. _draft((4,2), "RADIO-DISH")
  54. self.set('group', group)
  55. @property
  56. def routing_id(self):
  57. """The CLIENT-SERVER routing id of the message.
  58. Requires libzmq >= 4.2 and pyzmq built with draft APIs enabled.
  59. .. versionadded:: 17
  60. """
  61. _draft((4,2), "CLIENT-SERVER")
  62. return self.get('routing_id')
  63. @routing_id.setter
  64. def routing_id(self, routing_id):
  65. _draft((4,2), "CLIENT-SERVER")
  66. self.set('routing_id', routing_id)
  67. # keep deprecated alias
  68. Message = Frame
  69. __all__ = ['Frame', 'Message']