zmqstream.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. # Derived from iostream.py from tornado 1.0, Copyright 2009 Facebook
  2. # Used under Apache License Version 2.0
  3. #
  4. # Modifications are Copyright (C) PyZMQ Developers
  5. # Distributed under the terms of the Modified BSD License.
  6. """A utility class for event-based messaging on a zmq socket using tornado.
  7. .. seealso::
  8. - :mod:`zmq.asyncio`
  9. - :mod:`zmq.eventloop.future`
  10. """
  11. from __future__ import annotations
  12. import asyncio
  13. import pickle
  14. import warnings
  15. from queue import Queue
  16. from typing import Any, Awaitable, Callable, Literal, Sequence, cast, overload
  17. from tornado.ioloop import IOLoop
  18. from tornado.log import gen_log
  19. import zmq
  20. import zmq._future
  21. from zmq import POLLIN, POLLOUT
  22. from zmq.utils import jsonapi
  23. class ZMQStream:
  24. """A utility class to register callbacks when a zmq socket sends and receives
  25. For use with tornado IOLoop.
  26. There are three main methods
  27. Methods:
  28. * **on_recv(callback, copy=True):**
  29. register a callback to be run every time the socket has something to receive
  30. * **on_send(callback):**
  31. register a callback to be run every time you call send
  32. * **send_multipart(self, msg, flags=0, copy=False, callback=None):**
  33. perform a send that will trigger the callback
  34. if callback is passed, on_send is also called.
  35. There are also send_multipart(), send_json(), send_pyobj()
  36. Three other methods for deactivating the callbacks:
  37. * **stop_on_recv():**
  38. turn off the recv callback
  39. * **stop_on_send():**
  40. turn off the send callback
  41. which simply call ``on_<evt>(None)``.
  42. The entire socket interface, excluding direct recv methods, is also
  43. provided, primarily through direct-linking the methods.
  44. e.g.
  45. >>> stream.bind is stream.socket.bind
  46. True
  47. .. versionadded:: 25
  48. send/recv callbacks can be coroutines.
  49. .. versionchanged:: 25
  50. ZMQStreams only support base zmq.Socket classes (this has always been true, but not enforced).
  51. If ZMQStreams are created with e.g. async Socket subclasses,
  52. a RuntimeWarning will be shown,
  53. and the socket cast back to the default zmq.Socket
  54. before connecting events.
  55. Previously, using async sockets (or any zmq.Socket subclass) would result in undefined behavior for the
  56. arguments passed to callback functions.
  57. Now, the callback functions reliably get the return value of the base `zmq.Socket` send/recv_multipart methods
  58. (the list of message frames).
  59. """
  60. socket: zmq.Socket
  61. io_loop: IOLoop
  62. poller: zmq.Poller
  63. _send_queue: Queue
  64. _recv_callback: Callable | None
  65. _send_callback: Callable | None
  66. _close_callback: Callable | None
  67. _state: int = 0
  68. _flushed: bool = False
  69. _recv_copy: bool = False
  70. _fd: int
  71. def __init__(self, socket: zmq.Socket, io_loop: IOLoop | None = None):
  72. if isinstance(socket, zmq._future._AsyncSocket):
  73. warnings.warn(
  74. f"""ZMQStream only supports the base zmq.Socket class.
  75. Use zmq.Socket(shadow=other_socket)
  76. or `ctx.socket(zmq.{socket._type_name}, socket_class=zmq.Socket)`
  77. to create a base zmq.Socket object,
  78. no matter what other kind of socket your Context creates.
  79. """,
  80. RuntimeWarning,
  81. stacklevel=2,
  82. )
  83. # shadow back to base zmq.Socket,
  84. # otherwise callbacks like `on_recv` will get the wrong types.
  85. socket = zmq.Socket(shadow=socket)
  86. self.socket = socket
  87. # IOLoop.current() is deprecated if called outside the event loop
  88. # that means
  89. self.io_loop = io_loop or IOLoop.current()
  90. self.poller = zmq.Poller()
  91. self._fd = cast(int, self.socket.FD)
  92. self._send_queue = Queue()
  93. self._recv_callback = None
  94. self._send_callback = None
  95. self._close_callback = None
  96. self._recv_copy = False
  97. self._flushed = False
  98. self._state = 0
  99. self._init_io_state()
  100. # shortcircuit some socket methods
  101. self.bind = self.socket.bind
  102. self.bind_to_random_port = self.socket.bind_to_random_port
  103. self.connect = self.socket.connect
  104. self.setsockopt = self.socket.setsockopt
  105. self.getsockopt = self.socket.getsockopt
  106. self.setsockopt_string = self.socket.setsockopt_string
  107. self.getsockopt_string = self.socket.getsockopt_string
  108. self.setsockopt_unicode = self.socket.setsockopt_unicode
  109. self.getsockopt_unicode = self.socket.getsockopt_unicode
  110. def stop_on_recv(self):
  111. """Disable callback and automatic receiving."""
  112. return self.on_recv(None)
  113. def stop_on_send(self):
  114. """Disable callback on sending."""
  115. return self.on_send(None)
  116. def stop_on_err(self):
  117. """DEPRECATED, does nothing"""
  118. gen_log.warn("on_err does nothing, and will be removed")
  119. def on_err(self, callback: Callable):
  120. """DEPRECATED, does nothing"""
  121. gen_log.warn("on_err does nothing, and will be removed")
  122. @overload
  123. def on_recv(
  124. self,
  125. callback: Callable[[list[bytes]], Any],
  126. ) -> None: ...
  127. @overload
  128. def on_recv(
  129. self,
  130. callback: Callable[[list[bytes]], Any],
  131. copy: Literal[True],
  132. ) -> None: ...
  133. @overload
  134. def on_recv(
  135. self,
  136. callback: Callable[[list[zmq.Frame]], Any],
  137. copy: Literal[False],
  138. ) -> None: ...
  139. @overload
  140. def on_recv(
  141. self,
  142. callback: Callable[[list[zmq.Frame]], Any] | Callable[[list[bytes]], Any],
  143. copy: bool = ...,
  144. ): ...
  145. def on_recv(
  146. self,
  147. callback: Callable[[list[zmq.Frame]], Any] | Callable[[list[bytes]], Any],
  148. copy: bool = True,
  149. ) -> None:
  150. """Register a callback for when a message is ready to recv.
  151. There can be only one callback registered at a time, so each
  152. call to `on_recv` replaces previously registered callbacks.
  153. on_recv(None) disables recv event polling.
  154. Use on_recv_stream(callback) instead, to register a callback that will receive
  155. both this ZMQStream and the message, instead of just the message.
  156. Parameters
  157. ----------
  158. callback : callable
  159. callback must take exactly one argument, which will be a
  160. list, as returned by socket.recv_multipart()
  161. if callback is None, recv callbacks are disabled.
  162. copy : bool
  163. copy is passed directly to recv, so if copy is False,
  164. callback will receive Message objects. If copy is True,
  165. then callback will receive bytes/str objects.
  166. Returns : None
  167. """
  168. self._check_closed()
  169. assert callback is None or callable(callback)
  170. self._recv_callback = callback
  171. self._recv_copy = copy
  172. if callback is None:
  173. self._drop_io_state(zmq.POLLIN)
  174. else:
  175. self._add_io_state(zmq.POLLIN)
  176. @overload
  177. def on_recv_stream(
  178. self,
  179. callback: Callable[[ZMQStream, list[bytes]], Any],
  180. ) -> None: ...
  181. @overload
  182. def on_recv_stream(
  183. self,
  184. callback: Callable[[ZMQStream, list[bytes]], Any],
  185. copy: Literal[True],
  186. ) -> None: ...
  187. @overload
  188. def on_recv_stream(
  189. self,
  190. callback: Callable[[ZMQStream, list[zmq.Frame]], Any],
  191. copy: Literal[False],
  192. ) -> None: ...
  193. @overload
  194. def on_recv_stream(
  195. self,
  196. callback: (
  197. Callable[[ZMQStream, list[zmq.Frame]], Any]
  198. | Callable[[ZMQStream, list[bytes]], Any]
  199. ),
  200. copy: bool = ...,
  201. ): ...
  202. def on_recv_stream(
  203. self,
  204. callback: (
  205. Callable[[ZMQStream, list[zmq.Frame]], Any]
  206. | Callable[[ZMQStream, list[bytes]], Any]
  207. ),
  208. copy: bool = True,
  209. ):
  210. """Same as on_recv, but callback will get this stream as first argument
  211. callback must take exactly two arguments, as it will be called as::
  212. callback(stream, msg)
  213. Useful when a single callback should be used with multiple streams.
  214. """
  215. if callback is None:
  216. self.stop_on_recv()
  217. else:
  218. def stream_callback(msg):
  219. return callback(self, msg)
  220. self.on_recv(stream_callback, copy=copy)
  221. def on_send(
  222. self, callback: Callable[[Sequence[Any], zmq.MessageTracker | None], Any]
  223. ):
  224. """Register a callback to be called on each send
  225. There will be two arguments::
  226. callback(msg, status)
  227. * `msg` will be the list of sendable objects that was just sent
  228. * `status` will be the return result of socket.send_multipart(msg) -
  229. MessageTracker or None.
  230. Non-copying sends return a MessageTracker object whose
  231. `done` attribute will be True when the send is complete.
  232. This allows users to track when an object is safe to write to
  233. again.
  234. The second argument will always be None if copy=True
  235. on the send.
  236. Use on_send_stream(callback) to register a callback that will be passed
  237. this ZMQStream as the first argument, in addition to the other two.
  238. on_send(None) disables recv event polling.
  239. Parameters
  240. ----------
  241. callback : callable
  242. callback must take exactly two arguments, which will be
  243. the message being sent (always a list),
  244. and the return result of socket.send_multipart(msg) -
  245. MessageTracker or None.
  246. if callback is None, send callbacks are disabled.
  247. """
  248. self._check_closed()
  249. assert callback is None or callable(callback)
  250. self._send_callback = callback
  251. def on_send_stream(
  252. self,
  253. callback: Callable[[ZMQStream, Sequence[Any], zmq.MessageTracker | None], Any],
  254. ):
  255. """Same as on_send, but callback will get this stream as first argument
  256. Callback will be passed three arguments::
  257. callback(stream, msg, status)
  258. Useful when a single callback should be used with multiple streams.
  259. """
  260. if callback is None:
  261. self.stop_on_send()
  262. else:
  263. self.on_send(lambda msg, status: callback(self, msg, status))
  264. def send(self, msg, flags=0, copy=True, track=False, callback=None, **kwargs):
  265. """Send a message, optionally also register a new callback for sends.
  266. See zmq.socket.send for details.
  267. """
  268. return self.send_multipart(
  269. [msg], flags=flags, copy=copy, track=track, callback=callback, **kwargs
  270. )
  271. def send_multipart(
  272. self,
  273. msg: Sequence[Any],
  274. flags: int = 0,
  275. copy: bool = True,
  276. track: bool = False,
  277. callback: Callable | None = None,
  278. **kwargs: Any,
  279. ) -> None:
  280. """Send a multipart message, optionally also register a new callback for sends.
  281. See zmq.socket.send_multipart for details.
  282. """
  283. kwargs.update(dict(flags=flags, copy=copy, track=track))
  284. self._send_queue.put((msg, kwargs))
  285. callback = callback or self._send_callback
  286. if callback is not None:
  287. self.on_send(callback)
  288. else:
  289. # noop callback
  290. self.on_send(lambda *args: None)
  291. self._add_io_state(zmq.POLLOUT)
  292. def send_string(
  293. self,
  294. u: str,
  295. flags: int = 0,
  296. encoding: str = 'utf-8',
  297. callback: Callable | None = None,
  298. **kwargs: Any,
  299. ):
  300. """Send a unicode message with an encoding.
  301. See zmq.socket.send_unicode for details.
  302. """
  303. if not isinstance(u, str):
  304. raise TypeError("unicode/str objects only")
  305. return self.send(u.encode(encoding), flags=flags, callback=callback, **kwargs)
  306. send_unicode = send_string
  307. def send_json(
  308. self,
  309. obj: Any,
  310. flags: int = 0,
  311. callback: Callable | None = None,
  312. **kwargs: Any,
  313. ):
  314. """Send json-serialized version of an object.
  315. See zmq.socket.send_json for details.
  316. """
  317. msg = jsonapi.dumps(obj)
  318. return self.send(msg, flags=flags, callback=callback, **kwargs)
  319. def send_pyobj(
  320. self,
  321. obj: Any,
  322. flags: int = 0,
  323. protocol: int = -1,
  324. callback: Callable | None = None,
  325. **kwargs: Any,
  326. ):
  327. """Send a Python object as a message using pickle to serialize.
  328. See zmq.socket.send_json for details.
  329. """
  330. msg = pickle.dumps(obj, protocol)
  331. return self.send(msg, flags, callback=callback, **kwargs)
  332. def _finish_flush(self):
  333. """callback for unsetting _flushed flag."""
  334. self._flushed = False
  335. def flush(self, flag: int = zmq.POLLIN | zmq.POLLOUT, limit: int | None = None):
  336. """Flush pending messages.
  337. This method safely handles all pending incoming and/or outgoing messages,
  338. bypassing the inner loop, passing them to the registered callbacks.
  339. A limit can be specified, to prevent blocking under high load.
  340. flush will return the first time ANY of these conditions are met:
  341. * No more events matching the flag are pending.
  342. * the total number of events handled reaches the limit.
  343. Note that if ``flag|POLLIN != 0``, recv events will be flushed even if no callback
  344. is registered, unlike normal IOLoop operation. This allows flush to be
  345. used to remove *and ignore* incoming messages.
  346. Parameters
  347. ----------
  348. flag : int
  349. default=POLLIN|POLLOUT
  350. 0MQ poll flags.
  351. If flag|POLLIN, recv events will be flushed.
  352. If flag|POLLOUT, send events will be flushed.
  353. Both flags can be set at once, which is the default.
  354. limit : None or int, optional
  355. The maximum number of messages to send or receive.
  356. Both send and recv count against this limit.
  357. Returns
  358. -------
  359. int :
  360. count of events handled (both send and recv)
  361. """
  362. self._check_closed()
  363. # unset self._flushed, so callbacks will execute, in case flush has
  364. # already been called this iteration
  365. already_flushed = self._flushed
  366. self._flushed = False
  367. # initialize counters
  368. count = 0
  369. def update_flag():
  370. """Update the poll flag, to prevent registering POLLOUT events
  371. if we don't have pending sends."""
  372. return flag & zmq.POLLIN | (self.sending() and flag & zmq.POLLOUT)
  373. flag = update_flag()
  374. if not flag:
  375. # nothing to do
  376. return 0
  377. self.poller.register(self.socket, flag)
  378. events = self.poller.poll(0)
  379. while events and (not limit or count < limit):
  380. s, event = events[0]
  381. if event & POLLIN: # receiving
  382. self._handle_recv()
  383. count += 1
  384. if self.socket is None:
  385. # break if socket was closed during callback
  386. break
  387. if event & POLLOUT and self.sending():
  388. self._handle_send()
  389. count += 1
  390. if self.socket is None:
  391. # break if socket was closed during callback
  392. break
  393. flag = update_flag()
  394. if flag:
  395. self.poller.register(self.socket, flag)
  396. events = self.poller.poll(0)
  397. else:
  398. events = []
  399. if count: # only bypass loop if we actually flushed something
  400. # skip send/recv callbacks this iteration
  401. self._flushed = True
  402. # reregister them at the end of the loop
  403. if not already_flushed: # don't need to do it again
  404. self.io_loop.add_callback(self._finish_flush)
  405. elif already_flushed:
  406. self._flushed = True
  407. # update ioloop poll state, which may have changed
  408. self._rebuild_io_state()
  409. return count
  410. def set_close_callback(self, callback: Callable | None):
  411. """Call the given callback when the stream is closed."""
  412. self._close_callback = callback
  413. def close(self, linger: int | None = None) -> None:
  414. """Close this stream."""
  415. if self.socket is not None:
  416. if self.socket.closed:
  417. # fallback on raw fd for closed sockets
  418. # hopefully this happened promptly after close,
  419. # otherwise somebody else may have the FD
  420. warnings.warn(
  421. f"Unregistering FD {self._fd} after closing socket. "
  422. "This could result in unregistering handlers for the wrong socket. "
  423. "Please use stream.close() instead of closing the socket directly.",
  424. stacklevel=2,
  425. )
  426. self.io_loop.remove_handler(self._fd)
  427. else:
  428. self.io_loop.remove_handler(self.socket)
  429. self.socket.close(linger)
  430. self.socket = None # type: ignore
  431. if self._close_callback:
  432. self._run_callback(self._close_callback)
  433. def receiving(self) -> bool:
  434. """Returns True if we are currently receiving from the stream."""
  435. return self._recv_callback is not None
  436. def sending(self) -> bool:
  437. """Returns True if we are currently sending to the stream."""
  438. return not self._send_queue.empty()
  439. def closed(self) -> bool:
  440. if self.socket is None:
  441. return True
  442. if self.socket.closed:
  443. # underlying socket has been closed, but not by us!
  444. # trigger our cleanup
  445. self.close()
  446. return True
  447. return False
  448. def _run_callback(self, callback, *args, **kwargs):
  449. """Wrap running callbacks in try/except to allow us to
  450. close our socket."""
  451. try:
  452. f = callback(*args, **kwargs)
  453. if isinstance(f, Awaitable):
  454. f = asyncio.ensure_future(f)
  455. else:
  456. f = None
  457. except Exception:
  458. gen_log.error("Uncaught exception in ZMQStream callback", exc_info=True)
  459. # Re-raise the exception so that IOLoop.handle_callback_exception
  460. # can see it and log the error
  461. raise
  462. if f is not None:
  463. # handle async callbacks
  464. def _log_error(f):
  465. try:
  466. f.result()
  467. except Exception:
  468. gen_log.error(
  469. "Uncaught exception in ZMQStream callback", exc_info=True
  470. )
  471. f.add_done_callback(_log_error)
  472. def _handle_events(self, fd, events):
  473. """This method is the actual handler for IOLoop, that gets called whenever
  474. an event on my socket is posted. It dispatches to _handle_recv, etc."""
  475. if not self.socket:
  476. gen_log.warning("Got events for closed stream %s", self)
  477. return
  478. try:
  479. zmq_events = self.socket.EVENTS
  480. except zmq.ContextTerminated:
  481. gen_log.warning("Got events for stream %s after terminating context", self)
  482. # trigger close check, this will unregister callbacks
  483. self.closed()
  484. return
  485. except zmq.ZMQError as e:
  486. # run close check
  487. # shadow sockets may have been closed elsewhere,
  488. # which should show up as ENOTSOCK here
  489. if self.closed():
  490. gen_log.warning(
  491. "Got events for stream %s attached to closed socket: %s", self, e
  492. )
  493. else:
  494. gen_log.error("Error getting events for %s: %s", self, e)
  495. return
  496. try:
  497. # dispatch events:
  498. if zmq_events & zmq.POLLIN and self.receiving():
  499. self._handle_recv()
  500. if not self.socket:
  501. return
  502. if zmq_events & zmq.POLLOUT and self.sending():
  503. self._handle_send()
  504. if not self.socket:
  505. return
  506. # rebuild the poll state
  507. self._rebuild_io_state()
  508. except Exception:
  509. gen_log.error("Uncaught exception in zmqstream callback", exc_info=True)
  510. raise
  511. def _handle_recv(self):
  512. """Handle a recv event."""
  513. if self._flushed:
  514. return
  515. try:
  516. msg = self.socket.recv_multipart(zmq.NOBLOCK, copy=self._recv_copy)
  517. except zmq.ZMQError as e:
  518. if e.errno == zmq.EAGAIN:
  519. # state changed since poll event
  520. pass
  521. else:
  522. raise
  523. else:
  524. if self._recv_callback:
  525. callback = self._recv_callback
  526. self._run_callback(callback, msg)
  527. def _handle_send(self):
  528. """Handle a send event."""
  529. if self._flushed:
  530. return
  531. if not self.sending():
  532. gen_log.error("Shouldn't have handled a send event")
  533. return
  534. msg, kwargs = self._send_queue.get()
  535. try:
  536. status = self.socket.send_multipart(msg, **kwargs)
  537. except zmq.ZMQError as e:
  538. gen_log.error("SEND Error: %s", e)
  539. status = e
  540. if self._send_callback:
  541. callback = self._send_callback
  542. self._run_callback(callback, msg, status)
  543. def _check_closed(self):
  544. if not self.socket:
  545. raise OSError("Stream is closed")
  546. def _rebuild_io_state(self):
  547. """rebuild io state based on self.sending() and receiving()"""
  548. if self.socket is None:
  549. return
  550. state = 0
  551. if self.receiving():
  552. state |= zmq.POLLIN
  553. if self.sending():
  554. state |= zmq.POLLOUT
  555. self._state = state
  556. self._update_handler(state)
  557. def _add_io_state(self, state):
  558. """Add io_state to poller."""
  559. self._state = self._state | state
  560. self._update_handler(self._state)
  561. def _drop_io_state(self, state):
  562. """Stop poller from watching an io_state."""
  563. self._state = self._state & (~state)
  564. self._update_handler(self._state)
  565. def _update_handler(self, state):
  566. """Update IOLoop handler with state."""
  567. if self.socket is None:
  568. return
  569. if state & self.socket.events:
  570. # events still exist that haven't been processed
  571. # explicitly schedule handling to avoid missing events due to edge-triggered FDs
  572. self.io_loop.add_callback(lambda: self._handle_events(self.socket, 0))
  573. def _init_io_state(self):
  574. """initialize the ioloop event handler"""
  575. self.io_loop.add_handler(self.socket, self._handle_events, self.io_loop.READ)