socket.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. """0MQ Socket pure Python methods."""
  2. # Copyright (C) PyZMQ Developers
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import annotations
  5. import errno
  6. import pickle
  7. import random
  8. import sys
  9. from typing import (
  10. Any,
  11. Callable,
  12. Generic,
  13. List,
  14. Literal,
  15. Sequence,
  16. TypeVar,
  17. Union,
  18. cast,
  19. overload,
  20. )
  21. from warnings import warn
  22. import zmq
  23. from zmq._typing import TypeAlias
  24. from zmq.backend import Socket as SocketBase
  25. from zmq.error import ZMQBindError, ZMQError
  26. from zmq.utils import jsonapi
  27. from zmq.utils.interop import cast_int_addr
  28. from ..constants import SocketOption, SocketType, _OptType
  29. from .attrsettr import AttributeSetter
  30. from .poll import Poller
  31. try:
  32. DEFAULT_PROTOCOL = pickle.DEFAULT_PROTOCOL
  33. except AttributeError:
  34. DEFAULT_PROTOCOL = pickle.HIGHEST_PROTOCOL
  35. _SocketType = TypeVar("_SocketType", bound="Socket")
  36. _JSONType: TypeAlias = "int | str | bool | list[_JSONType] | dict[str, _JSONType]"
  37. class _SocketContext(Generic[_SocketType]):
  38. """Context Manager for socket bind/unbind"""
  39. socket: _SocketType
  40. kind: str
  41. addr: str
  42. def __repr__(self):
  43. return f"<SocketContext({self.kind}={self.addr!r})>"
  44. def __init__(
  45. self: _SocketContext[_SocketType], socket: _SocketType, kind: str, addr: str
  46. ):
  47. assert kind in {"bind", "connect"}
  48. self.socket = socket
  49. self.kind = kind
  50. self.addr = addr
  51. def __enter__(self: _SocketContext[_SocketType]) -> _SocketType:
  52. return self.socket
  53. def __exit__(self, *args):
  54. if self.socket.closed:
  55. return
  56. if self.kind == "bind":
  57. self.socket.unbind(self.addr)
  58. elif self.kind == "connect":
  59. self.socket.disconnect(self.addr)
  60. SocketReturnType = TypeVar("SocketReturnType")
  61. class Socket(SocketBase, AttributeSetter, Generic[SocketReturnType]):
  62. """The ZMQ socket object
  63. To create a Socket, first create a Context::
  64. ctx = zmq.Context.instance()
  65. then call ``ctx.socket(socket_type)``::
  66. s = ctx.socket(zmq.ROUTER)
  67. .. versionadded:: 25
  68. Sockets can now be shadowed by passing another Socket.
  69. This helps in creating an async copy of a sync socket or vice versa::
  70. s = zmq.Socket(async_socket)
  71. Which previously had to be::
  72. s = zmq.Socket.shadow(async_socket.underlying)
  73. """
  74. _shadow = False
  75. _shadow_obj = None
  76. _monitor_socket = None
  77. _type_name = 'UNKNOWN'
  78. @overload
  79. def __init__(
  80. self: Socket[bytes],
  81. ctx_or_socket: zmq.Context,
  82. socket_type: int,
  83. *,
  84. copy_threshold: int | None = None,
  85. ): ...
  86. @overload
  87. def __init__(
  88. self: Socket[bytes],
  89. *,
  90. shadow: Socket | int,
  91. copy_threshold: int | None = None,
  92. ): ...
  93. @overload
  94. def __init__(
  95. self: Socket[bytes],
  96. ctx_or_socket: Socket,
  97. ): ...
  98. def __init__(
  99. self: Socket[bytes],
  100. ctx_or_socket: zmq.Context | Socket | None = None,
  101. socket_type: int = 0,
  102. *,
  103. shadow: Socket | int = 0,
  104. copy_threshold: int | None = None,
  105. ):
  106. shadow_context: zmq.Context | None = None
  107. if isinstance(ctx_or_socket, zmq.Socket):
  108. # positional Socket(other_socket)
  109. shadow = ctx_or_socket
  110. ctx_or_socket = None
  111. shadow_address: int = 0
  112. if shadow:
  113. self._shadow = True
  114. # hold a reference to the shadow object
  115. self._shadow_obj = shadow
  116. if not isinstance(shadow, int):
  117. if isinstance(shadow, zmq.Socket):
  118. shadow_context = shadow.context
  119. try:
  120. shadow = cast(int, shadow.underlying)
  121. except AttributeError:
  122. pass
  123. shadow_address = cast_int_addr(shadow)
  124. else:
  125. self._shadow = False
  126. super().__init__(
  127. ctx_or_socket,
  128. socket_type,
  129. shadow=shadow_address,
  130. copy_threshold=copy_threshold,
  131. )
  132. if self._shadow_obj and shadow_context:
  133. # keep self.context reference if shadowing a Socket object
  134. self.context = shadow_context
  135. try:
  136. socket_type = cast(int, self.get(zmq.TYPE))
  137. except Exception:
  138. pass
  139. else:
  140. try:
  141. self.__dict__["type"] = stype = SocketType(socket_type)
  142. except ValueError:
  143. self._type_name = str(socket_type)
  144. else:
  145. self._type_name = stype.name
  146. def __del__(self):
  147. if not self._shadow and not self.closed:
  148. if warn is not None:
  149. # warn can be None during process teardown
  150. warn(
  151. f"Unclosed socket {self}",
  152. ResourceWarning,
  153. stacklevel=2,
  154. source=self,
  155. )
  156. self.close()
  157. _repr_cls = "zmq.Socket"
  158. def __repr__(self):
  159. cls = self.__class__
  160. # look up _repr_cls on exact class, not inherited
  161. _repr_cls = cls.__dict__.get("_repr_cls", None)
  162. if _repr_cls is None:
  163. _repr_cls = f"{cls.__module__}.{cls.__name__}"
  164. closed = ' closed' if self._closed else ''
  165. return f"<{_repr_cls}(zmq.{self._type_name}) at {hex(id(self))}{closed}>"
  166. # socket as context manager:
  167. def __enter__(self: _SocketType) -> _SocketType:
  168. """Sockets are context managers
  169. .. versionadded:: 14.4
  170. """
  171. return self
  172. def __exit__(self, *args, **kwargs):
  173. self.close()
  174. # -------------------------------------------------------------------------
  175. # Socket creation
  176. # -------------------------------------------------------------------------
  177. def __copy__(self: _SocketType, memo=None) -> _SocketType:
  178. """Copying a Socket creates a shadow copy"""
  179. return self.__class__.shadow(self.underlying)
  180. __deepcopy__ = __copy__
  181. @classmethod
  182. def shadow(cls: type[_SocketType], address: int | zmq.Socket) -> _SocketType:
  183. """Shadow an existing libzmq socket
  184. address is a zmq.Socket or an integer (or FFI pointer)
  185. representing the address of the libzmq socket.
  186. .. versionadded:: 14.1
  187. .. versionadded:: 25
  188. Support for shadowing `zmq.Socket` objects,
  189. instead of just integer addresses.
  190. """
  191. return cls(shadow=address)
  192. def close(self, linger=None) -> None:
  193. """
  194. Close the socket.
  195. If linger is specified, LINGER sockopt will be set prior to closing.
  196. Note: closing a zmq Socket may not close the underlying sockets
  197. if there are undelivered messages.
  198. Only after all messages are delivered or discarded by reaching the socket's LINGER timeout
  199. (default: forever)
  200. will the underlying sockets be closed.
  201. This can be called to close the socket by hand. If this is not
  202. called, the socket will automatically be closed when it is
  203. garbage collected,
  204. in which case you may see a ResourceWarning about the unclosed socket.
  205. """
  206. if self.context:
  207. self.context._rm_socket(self)
  208. super().close(linger=linger)
  209. # -------------------------------------------------------------------------
  210. # Connect/Bind context managers
  211. # -------------------------------------------------------------------------
  212. def _connect_cm(self: _SocketType, addr: str) -> _SocketContext[_SocketType]:
  213. """Context manager to disconnect on exit
  214. .. versionadded:: 20.0
  215. """
  216. return _SocketContext(self, 'connect', addr)
  217. def _bind_cm(self: _SocketType, addr: str) -> _SocketContext[_SocketType]:
  218. """Context manager to unbind on exit
  219. .. versionadded:: 20.0
  220. """
  221. try:
  222. # retrieve last_endpoint
  223. # to support binding on random ports via
  224. # `socket.bind('tcp://127.0.0.1:0')`
  225. addr = cast(bytes, self.get(zmq.LAST_ENDPOINT)).decode("utf8")
  226. except (AttributeError, ZMQError, UnicodeDecodeError):
  227. pass
  228. return _SocketContext(self, 'bind', addr)
  229. def bind(self: _SocketType, addr: str) -> _SocketContext[_SocketType]:
  230. """s.bind(addr)
  231. Bind the socket to an address.
  232. This causes the socket to listen on a network port. Sockets on the
  233. other side of this connection will use ``Socket.connect(addr)`` to
  234. connect to this socket.
  235. Returns a context manager which will call unbind on exit.
  236. .. versionadded:: 20.0
  237. Can be used as a context manager.
  238. .. versionadded:: 26.0
  239. binding to port 0 can be used as a context manager
  240. for binding to a random port.
  241. The URL can be retrieved as `socket.last_endpoint`.
  242. Parameters
  243. ----------
  244. addr : str
  245. The address string. This has the form 'protocol://interface:port',
  246. for example 'tcp://127.0.0.1:5555'. Protocols supported include
  247. tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is
  248. encoded to utf-8 first.
  249. """
  250. try:
  251. super().bind(addr)
  252. except ZMQError as e:
  253. e.strerror += f" (addr={addr!r})"
  254. raise
  255. return self._bind_cm(addr)
  256. def connect(self: _SocketType, addr: str) -> _SocketContext[_SocketType]:
  257. """s.connect(addr)
  258. Connect to a remote 0MQ socket.
  259. Returns a context manager which will call disconnect on exit.
  260. .. versionadded:: 20.0
  261. Can be used as a context manager.
  262. Parameters
  263. ----------
  264. addr : str
  265. The address string. This has the form 'protocol://interface:port',
  266. for example 'tcp://127.0.0.1:5555'. Protocols supported are
  267. tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
  268. encoded to utf-8 first.
  269. """
  270. try:
  271. super().connect(addr)
  272. except ZMQError as e:
  273. e.strerror += f" (addr={addr!r})"
  274. raise
  275. return self._connect_cm(addr)
  276. # -------------------------------------------------------------------------
  277. # Deprecated aliases
  278. # -------------------------------------------------------------------------
  279. @property
  280. def socket_type(self) -> int:
  281. warn("Socket.socket_type is deprecated, use Socket.type", DeprecationWarning)
  282. return cast(int, self.type)
  283. # -------------------------------------------------------------------------
  284. # Hooks for sockopt completion
  285. # -------------------------------------------------------------------------
  286. def __dir__(self):
  287. keys = dir(self.__class__)
  288. keys.extend(SocketOption.__members__)
  289. return keys
  290. # -------------------------------------------------------------------------
  291. # Getting/Setting options
  292. # -------------------------------------------------------------------------
  293. setsockopt = SocketBase.set
  294. getsockopt = SocketBase.get
  295. def __setattr__(self, key, value):
  296. """Override to allow setting zmq.[UN]SUBSCRIBE even though we have a subscribe method"""
  297. if key in self.__dict__:
  298. object.__setattr__(self, key, value)
  299. return
  300. _key = key.lower()
  301. if _key in ('subscribe', 'unsubscribe'):
  302. if isinstance(value, str):
  303. value = value.encode('utf8')
  304. if _key == 'subscribe':
  305. self.set(zmq.SUBSCRIBE, value)
  306. else:
  307. self.set(zmq.UNSUBSCRIBE, value)
  308. return
  309. super().__setattr__(key, value)
  310. def fileno(self) -> int:
  311. """Return edge-triggered file descriptor for this socket.
  312. This is a read-only edge-triggered file descriptor for both read and write events on this socket.
  313. It is important that all available events be consumed when an event is detected,
  314. otherwise the read event will not trigger again.
  315. .. versionadded:: 17.0
  316. """
  317. return self.FD
  318. def subscribe(self, topic: str | bytes) -> None:
  319. """Subscribe to a topic
  320. Only for SUB sockets.
  321. .. versionadded:: 15.3
  322. """
  323. if isinstance(topic, str):
  324. topic = topic.encode('utf8')
  325. self.set(zmq.SUBSCRIBE, topic)
  326. def unsubscribe(self, topic: str | bytes) -> None:
  327. """Unsubscribe from a topic
  328. Only for SUB sockets.
  329. .. versionadded:: 15.3
  330. """
  331. if isinstance(topic, str):
  332. topic = topic.encode('utf8')
  333. self.set(zmq.UNSUBSCRIBE, topic)
  334. def set_string(self, option: int, optval: str, encoding='utf-8') -> None:
  335. """Set socket options with a unicode object.
  336. This is simply a wrapper for setsockopt to protect from encoding ambiguity.
  337. See the 0MQ documentation for details on specific options.
  338. Parameters
  339. ----------
  340. option : int
  341. The name of the option to set. Can be any of: SUBSCRIBE,
  342. UNSUBSCRIBE, IDENTITY
  343. optval : str
  344. The value of the option to set.
  345. encoding : str
  346. The encoding to be used, default is utf8
  347. """
  348. if not isinstance(optval, str):
  349. raise TypeError(f"strings only, not {type(optval)}: {optval!r}")
  350. return self.set(option, optval.encode(encoding))
  351. setsockopt_unicode = setsockopt_string = set_string
  352. def get_string(self, option: int, encoding='utf-8') -> str:
  353. """Get the value of a socket option.
  354. See the 0MQ documentation for details on specific options.
  355. Parameters
  356. ----------
  357. option : int
  358. The option to retrieve.
  359. Returns
  360. -------
  361. optval : str
  362. The value of the option as a unicode string.
  363. """
  364. if SocketOption(option)._opt_type != _OptType.bytes:
  365. raise TypeError(f"option {option} will not return a string to be decoded")
  366. return cast(bytes, self.get(option)).decode(encoding)
  367. getsockopt_unicode = getsockopt_string = get_string
  368. def bind_to_random_port(
  369. self: _SocketType,
  370. addr: str,
  371. min_port: int = 49152,
  372. max_port: int = 65536,
  373. max_tries: int = 100,
  374. ) -> int:
  375. """Bind this socket to a random port in a range.
  376. If the port range is unspecified, the system will choose the port.
  377. Parameters
  378. ----------
  379. addr : str
  380. The address string without the port to pass to ``Socket.bind()``.
  381. min_port : int, optional
  382. The minimum port in the range of ports to try (inclusive).
  383. max_port : int, optional
  384. The maximum port in the range of ports to try (exclusive).
  385. max_tries : int, optional
  386. The maximum number of bind attempts to make.
  387. Returns
  388. -------
  389. port : int
  390. The port the socket was bound to.
  391. Raises
  392. ------
  393. ZMQBindError
  394. if `max_tries` reached before successful bind
  395. """
  396. if min_port == 49152 and max_port == 65536:
  397. # if LAST_ENDPOINT is supported, and min_port / max_port weren't specified,
  398. # we can bind to port 0 and let the OS do the work
  399. self.bind(f"{addr}:*")
  400. url = cast(bytes, self.last_endpoint).decode('ascii', 'replace')
  401. _, port_s = url.rsplit(':', 1)
  402. return int(port_s)
  403. for i in range(max_tries):
  404. try:
  405. port = random.randrange(min_port, max_port)
  406. self.bind(f'{addr}:{port}')
  407. except ZMQError as exception:
  408. en = exception.errno
  409. if en == zmq.EADDRINUSE:
  410. continue
  411. elif sys.platform == 'win32' and en == errno.EACCES:
  412. continue
  413. else:
  414. raise
  415. else:
  416. return port
  417. raise ZMQBindError("Could not bind socket to random port.")
  418. def get_hwm(self) -> int:
  419. """Get the High Water Mark.
  420. On libzmq ≥ 3, this gets SNDHWM if available, otherwise RCVHWM
  421. """
  422. # return sndhwm, fallback on rcvhwm
  423. try:
  424. return cast(int, self.get(zmq.SNDHWM))
  425. except zmq.ZMQError:
  426. pass
  427. return cast(int, self.get(zmq.RCVHWM))
  428. def set_hwm(self, value: int) -> None:
  429. """Set the High Water Mark.
  430. On libzmq ≥ 3, this sets both SNDHWM and RCVHWM
  431. .. warning::
  432. New values only take effect for subsequent socket
  433. bind/connects.
  434. """
  435. raised = None
  436. try:
  437. self.sndhwm = value
  438. except Exception as e:
  439. raised = e
  440. try:
  441. self.rcvhwm = value
  442. except Exception as e:
  443. raised = e
  444. if raised:
  445. raise raised
  446. hwm = property(
  447. get_hwm,
  448. set_hwm,
  449. None,
  450. """Property for High Water Mark.
  451. Setting hwm sets both SNDHWM and RCVHWM as appropriate.
  452. It gets SNDHWM if available, otherwise RCVHWM.
  453. """,
  454. )
  455. # -------------------------------------------------------------------------
  456. # Sending and receiving messages
  457. # -------------------------------------------------------------------------
  458. @overload
  459. def send(
  460. self,
  461. data: Any,
  462. flags: int = ...,
  463. copy: bool = ...,
  464. *,
  465. track: Literal[True],
  466. routing_id: int | None = ...,
  467. group: str | None = ...,
  468. ) -> zmq.MessageTracker: ...
  469. @overload
  470. def send(
  471. self,
  472. data: Any,
  473. flags: int = ...,
  474. copy: bool = ...,
  475. *,
  476. track: Literal[False],
  477. routing_id: int | None = ...,
  478. group: str | None = ...,
  479. ) -> None: ...
  480. @overload
  481. def send(
  482. self,
  483. data: Any,
  484. flags: int = ...,
  485. *,
  486. copy: bool = ...,
  487. routing_id: int | None = ...,
  488. group: str | None = ...,
  489. ) -> None: ...
  490. @overload
  491. def send(
  492. self,
  493. data: Any,
  494. flags: int = ...,
  495. copy: bool = ...,
  496. track: bool = ...,
  497. routing_id: int | None = ...,
  498. group: str | None = ...,
  499. ) -> zmq.MessageTracker | None: ...
  500. def send(
  501. self,
  502. data: Any,
  503. flags: int = 0,
  504. copy: bool = True,
  505. track: bool = False,
  506. routing_id: int | None = None,
  507. group: str | None = None,
  508. ) -> zmq.MessageTracker | None:
  509. """Send a single zmq message frame on this socket.
  510. This queues the message to be sent by the IO thread at a later time.
  511. With flags=NOBLOCK, this raises :class:`ZMQError` if the queue is full;
  512. otherwise, this waits until space is available.
  513. See :class:`Poller` for more general non-blocking I/O.
  514. Parameters
  515. ----------
  516. data : bytes, Frame, memoryview
  517. The content of the message. This can be any object that provides
  518. the Python buffer API (i.e. `memoryview(data)` can be called).
  519. flags : int
  520. 0, NOBLOCK, SNDMORE, or NOBLOCK|SNDMORE.
  521. copy : bool
  522. Should the message be sent in a copying or non-copying manner.
  523. track : bool
  524. Should the message be tracked for notification that ZMQ has
  525. finished with it? (ignored if copy=True)
  526. routing_id : int
  527. For use with SERVER sockets
  528. group : str
  529. For use with RADIO sockets
  530. Returns
  531. -------
  532. None : if `copy` or not track
  533. None if message was sent, raises an exception otherwise.
  534. MessageTracker : if track and not copy
  535. a MessageTracker object, whose `done` property will
  536. be False until the send is completed.
  537. Raises
  538. ------
  539. TypeError
  540. If a unicode object is passed
  541. ValueError
  542. If `track=True`, but an untracked Frame is passed.
  543. ZMQError
  544. If the send does not succeed for any reason (including
  545. if NOBLOCK is set and the outgoing queue is full).
  546. .. versionchanged:: 17.0
  547. DRAFT support for routing_id and group arguments.
  548. """
  549. if routing_id is not None:
  550. if not isinstance(data, zmq.Frame):
  551. data = zmq.Frame(
  552. data,
  553. track=track,
  554. copy=copy or None,
  555. copy_threshold=self.copy_threshold,
  556. )
  557. data.routing_id = routing_id
  558. if group is not None:
  559. if not isinstance(data, zmq.Frame):
  560. data = zmq.Frame(
  561. data,
  562. track=track,
  563. copy=copy or None,
  564. copy_threshold=self.copy_threshold,
  565. )
  566. data.group = group
  567. return super().send(data, flags=flags, copy=copy, track=track)
  568. def send_multipart(
  569. self,
  570. msg_parts: Sequence,
  571. flags: int = 0,
  572. copy: bool = True,
  573. track: bool = False,
  574. **kwargs,
  575. ):
  576. """Send a sequence of buffers as a multipart message.
  577. The zmq.SNDMORE flag is added to all msg parts before the last.
  578. Parameters
  579. ----------
  580. msg_parts : iterable
  581. A sequence of objects to send as a multipart message. Each element
  582. can be any sendable object (Frame, bytes, buffer-providers)
  583. flags : int, optional
  584. Any valid flags for :func:`Socket.send`.
  585. SNDMORE is added automatically for frames before the last.
  586. copy : bool, optional
  587. Should the frame(s) be sent in a copying or non-copying manner.
  588. If copy=False, frames smaller than self.copy_threshold bytes
  589. will be copied anyway.
  590. track : bool, optional
  591. Should the frame(s) be tracked for notification that ZMQ has
  592. finished with it (ignored if copy=True).
  593. Returns
  594. -------
  595. None : if copy or not track
  596. MessageTracker : if track and not copy
  597. a MessageTracker object, whose `done` property will
  598. be False until the last send is completed.
  599. """
  600. # typecheck parts before sending:
  601. for i, msg in enumerate(msg_parts):
  602. if isinstance(msg, (zmq.Frame, bytes, memoryview)):
  603. continue
  604. try:
  605. memoryview(msg)
  606. except Exception:
  607. rmsg = repr(msg)
  608. if len(rmsg) > 32:
  609. rmsg = rmsg[:32] + '...'
  610. raise TypeError(
  611. f"Frame {i} ({rmsg}) does not support the buffer interface."
  612. )
  613. for msg in msg_parts[:-1]:
  614. self.send(msg, zmq.SNDMORE | flags, copy=copy, track=track)
  615. # Send the last part without the extra SNDMORE flag.
  616. return self.send(msg_parts[-1], flags, copy=copy, track=track)
  617. @overload
  618. def recv_multipart(
  619. self, flags: int = ..., *, copy: Literal[True], track: bool = ...
  620. ) -> list[bytes]: ...
  621. @overload
  622. def recv_multipart(
  623. self, flags: int = ..., *, copy: Literal[False], track: bool = ...
  624. ) -> list[zmq.Frame]: ...
  625. @overload
  626. def recv_multipart(self, flags: int = ..., *, track: bool = ...) -> list[bytes]: ...
  627. @overload
  628. def recv_multipart(
  629. self, flags: int = 0, copy: bool = True, track: bool = False
  630. ) -> list[zmq.Frame] | list[bytes]: ...
  631. def recv_multipart(
  632. self, flags: int = 0, copy: bool = True, track: bool = False
  633. ) -> list[zmq.Frame] | list[bytes]:
  634. """Receive a multipart message as a list of bytes or Frame objects
  635. Parameters
  636. ----------
  637. flags : int, optional
  638. Any valid flags for :func:`Socket.recv`.
  639. copy : bool, optional
  640. Should the message frame(s) be received in a copying or non-copying manner?
  641. If False a Frame object is returned for each part, if True a copy of
  642. the bytes is made for each frame.
  643. track : bool, optional
  644. Should the message frame(s) be tracked for notification that ZMQ has
  645. finished with it? (ignored if copy=True)
  646. Returns
  647. -------
  648. msg_parts : list
  649. A list of frames in the multipart message; either Frames or bytes,
  650. depending on `copy`.
  651. Raises
  652. ------
  653. ZMQError
  654. for any of the reasons :func:`~Socket.recv` might fail
  655. """
  656. parts = [self.recv(flags, copy=copy, track=track)]
  657. # have first part already, only loop while more to receive
  658. while self.getsockopt(zmq.RCVMORE):
  659. part = self.recv(flags, copy=copy, track=track)
  660. parts.append(part)
  661. # cast List[Union] to Union[List]
  662. # how do we get mypy to recognize that return type is invariant on `copy`?
  663. return cast(Union[List[zmq.Frame], List[bytes]], parts)
  664. def _deserialize(
  665. self,
  666. recvd: bytes,
  667. load: Callable[[bytes], Any],
  668. ) -> Any:
  669. """Deserialize a received message
  670. Override in subclass (e.g. Futures) if recvd is not the raw bytes.
  671. The default implementation expects bytes and returns the deserialized message immediately.
  672. Parameters
  673. ----------
  674. load: callable
  675. Callable that deserializes bytes
  676. recvd:
  677. The object returned by self.recv
  678. """
  679. return load(recvd)
  680. def send_serialized(self, msg, serialize, flags=0, copy=True, **kwargs):
  681. """Send a message with a custom serialization function.
  682. .. versionadded:: 17
  683. Parameters
  684. ----------
  685. msg : The message to be sent. Can be any object serializable by `serialize`.
  686. serialize : callable
  687. The serialization function to use.
  688. serialize(msg) should return an iterable of sendable message frames
  689. (e.g. bytes objects), which will be passed to send_multipart.
  690. flags : int, optional
  691. Any valid flags for :func:`Socket.send`.
  692. copy : bool, optional
  693. Whether to copy the frames.
  694. """
  695. frames = serialize(msg)
  696. return self.send_multipart(frames, flags=flags, copy=copy, **kwargs)
  697. def recv_serialized(self, deserialize, flags=0, copy=True):
  698. """Receive a message with a custom deserialization function.
  699. .. versionadded:: 17
  700. Parameters
  701. ----------
  702. deserialize : callable
  703. The deserialization function to use.
  704. deserialize will be called with one argument: the list of frames
  705. returned by recv_multipart() and can return any object.
  706. flags : int, optional
  707. Any valid flags for :func:`Socket.recv`.
  708. copy : bool, optional
  709. Whether to recv bytes or Frame objects.
  710. Returns
  711. -------
  712. obj : object
  713. The object returned by the deserialization function.
  714. Raises
  715. ------
  716. ZMQError
  717. for any of the reasons :func:`~Socket.recv` might fail
  718. """
  719. frames = self.recv_multipart(flags=flags, copy=copy)
  720. return self._deserialize(frames, deserialize)
  721. def send_string(
  722. self,
  723. u: str,
  724. flags: int = 0,
  725. copy: bool = True,
  726. encoding: str = 'utf-8',
  727. **kwargs,
  728. ) -> zmq.Frame | None:
  729. """Send a Python unicode string as a message with an encoding.
  730. 0MQ communicates with raw bytes, so you must encode/decode
  731. text (str) around 0MQ.
  732. Parameters
  733. ----------
  734. u : str
  735. The unicode string to send.
  736. flags : int, optional
  737. Any valid flags for :func:`Socket.send`.
  738. encoding : str
  739. The encoding to be used
  740. """
  741. if not isinstance(u, str):
  742. raise TypeError("str objects only")
  743. return self.send(u.encode(encoding), flags=flags, copy=copy, **kwargs)
  744. send_unicode = send_string
  745. def recv_string(self, flags: int = 0, encoding: str = 'utf-8') -> str:
  746. """Receive a unicode string, as sent by send_string.
  747. Parameters
  748. ----------
  749. flags : int
  750. Any valid flags for :func:`Socket.recv`.
  751. encoding : str
  752. The encoding to be used
  753. Returns
  754. -------
  755. s : str
  756. The Python unicode string that arrives as encoded bytes.
  757. Raises
  758. ------
  759. ZMQError
  760. for any of the reasons :func:`Socket.recv` might fail
  761. """
  762. msg = self.recv(flags=flags)
  763. return self._deserialize(msg, lambda buf: buf.decode(encoding))
  764. recv_unicode = recv_string
  765. def send_pyobj(
  766. self, obj: Any, flags: int = 0, protocol: int = DEFAULT_PROTOCOL, **kwargs
  767. ) -> zmq.Frame | None:
  768. """
  769. Send a Python object as a message using pickle to serialize.
  770. .. warning::
  771. Never deserialize an untrusted message with pickle,
  772. which can involve arbitrary code execution.
  773. Make sure to authenticate the sources of messages
  774. before unpickling them, e.g. with transport-level security
  775. (e.g. CURVE, ZAP, or IPC permissions)
  776. or signed messages.
  777. Parameters
  778. ----------
  779. obj : Python object
  780. The Python object to send.
  781. flags : int
  782. Any valid flags for :func:`Socket.send`.
  783. protocol : int
  784. The pickle protocol number to use. The default is pickle.DEFAULT_PROTOCOL
  785. where defined, and pickle.HIGHEST_PROTOCOL elsewhere.
  786. """
  787. msg = pickle.dumps(obj, protocol)
  788. return self.send(msg, flags=flags, **kwargs)
  789. def recv_pyobj(self, flags: int = 0) -> Any:
  790. """
  791. Receive a Python object as a message using UNSAFE pickle to serialize.
  792. .. warning::
  793. Never deserialize an untrusted message with pickle,
  794. which can involve arbitrary code execution.
  795. Make sure to authenticate the sources of messages
  796. before unpickling them, e.g. with transport-level security
  797. (such as CURVE or IPC permissions)
  798. or authenticating messages themselves before deserializing.
  799. Parameters
  800. ----------
  801. flags : int
  802. Any valid flags for :func:`Socket.recv`.
  803. Returns
  804. -------
  805. obj : Python object
  806. The Python object that arrives as a message.
  807. Raises
  808. ------
  809. ZMQError
  810. for any of the reasons :func:`~Socket.recv` might fail
  811. """
  812. msg = self.recv(flags)
  813. return self._deserialize(msg, pickle.loads)
  814. def send_json(self, obj: Any, flags: int = 0, **kwargs) -> None:
  815. """Send a Python object as a message using json to serialize.
  816. Keyword arguments are passed on to json.dumps
  817. Parameters
  818. ----------
  819. obj : Python object
  820. The Python object to send
  821. flags : int
  822. Any valid flags for :func:`Socket.send`
  823. """
  824. send_kwargs = {}
  825. for key in ('routing_id', 'group'):
  826. if key in kwargs:
  827. send_kwargs[key] = kwargs.pop(key)
  828. msg = jsonapi.dumps(obj, **kwargs)
  829. return self.send(msg, flags=flags, **send_kwargs)
  830. def recv_json(self, flags: int = 0, **kwargs) -> _JSONType:
  831. """Receive a Python object as a message using json to serialize.
  832. Keyword arguments are passed on to json.loads
  833. Parameters
  834. ----------
  835. flags : int
  836. Any valid flags for :func:`Socket.recv`.
  837. Returns
  838. -------
  839. obj : Python object
  840. The Python object that arrives as a message.
  841. Raises
  842. ------
  843. ZMQError
  844. for any of the reasons :func:`~Socket.recv` might fail
  845. """
  846. msg = self.recv(flags)
  847. return self._deserialize(msg, lambda buf: jsonapi.loads(buf, **kwargs))
  848. _poller_class = Poller
  849. def poll(self, timeout: int | None = None, flags: int = zmq.POLLIN) -> int:
  850. """Poll the socket for events.
  851. See :class:`Poller` to wait for multiple sockets at once.
  852. Parameters
  853. ----------
  854. timeout : int
  855. The timeout (in milliseconds) to wait for an event. If unspecified
  856. (or specified None), will wait forever for an event.
  857. flags : int
  858. default: POLLIN.
  859. POLLIN, POLLOUT, or POLLIN|POLLOUT. The event flags to poll for.
  860. Returns
  861. -------
  862. event_mask : int
  863. The poll event mask (POLLIN, POLLOUT),
  864. 0 if the timeout was reached without an event.
  865. """
  866. if self.closed:
  867. raise ZMQError(zmq.ENOTSUP)
  868. p = self._poller_class()
  869. p.register(self, flags)
  870. evts = dict(p.poll(timeout))
  871. # return 0 if no events, otherwise return event bitfield
  872. return evts.get(self, 0)
  873. def get_monitor_socket(
  874. self: _SocketType, events: int | None = None, addr: str | None = None
  875. ) -> _SocketType:
  876. """Return a connected PAIR socket ready to receive the event notifications.
  877. .. versionadded:: libzmq-4.0
  878. .. versionadded:: 14.0
  879. Parameters
  880. ----------
  881. events : int
  882. default: `zmq.EVENT_ALL`
  883. The bitmask defining which events are wanted.
  884. addr : str
  885. The optional endpoint for the monitoring sockets.
  886. Returns
  887. -------
  888. socket : zmq.Socket
  889. The PAIR socket, connected and ready to receive messages.
  890. """
  891. # safe-guard, method only available on libzmq >= 4
  892. if zmq.zmq_version_info() < (4,):
  893. raise NotImplementedError(
  894. f"get_monitor_socket requires libzmq >= 4, have {zmq.zmq_version()}"
  895. )
  896. # if already monitoring, return existing socket
  897. if self._monitor_socket:
  898. if self._monitor_socket.closed:
  899. self._monitor_socket = None
  900. else:
  901. return self._monitor_socket
  902. if addr is None:
  903. # create endpoint name from internal fd
  904. addr = f"inproc://monitor.s-{self.FD}"
  905. if events is None:
  906. # use all events
  907. events = zmq.EVENT_ALL
  908. # attach monitoring socket
  909. self.monitor(addr, events)
  910. # create new PAIR socket and connect it
  911. self._monitor_socket = self.context.socket(zmq.PAIR)
  912. self._monitor_socket.connect(addr)
  913. return self._monitor_socket
  914. def disable_monitor(self) -> None:
  915. """Shutdown the PAIR socket (created using get_monitor_socket)
  916. that is serving socket events.
  917. .. versionadded:: 14.4
  918. """
  919. self._monitor_socket = None
  920. self.monitor(None, 0)
  921. SyncSocket: TypeAlias = Socket[bytes]
  922. __all__ = ['Socket', 'SyncSocket']