eventloop.zmqstream#
Module: zmq.eventloop.zmqstream
#
A utility class for event-based messaging on a zmq socket using tornado.
See also
ZMQStream
#
- class zmq.eventloop.zmqstream.ZMQStream(socket: Socket, io_loop: IOLoop | None = None)#
A utility class to register callbacks when a zmq socket sends and receives
For use with tornado IOLoop.
There are three main methods
Methods:
- on_recv(callback, copy=True):
register a callback to be run every time the socket has something to receive
- on_send(callback):
register a callback to be run every time you call send
- send_multipart(self, msg, flags=0, copy=False, callback=None):
perform a send that will trigger the callback if callback is passed, on_send is also called.
There are also send_multipart(), send_json(), send_pyobj()
Three other methods for deactivating the callbacks:
- stop_on_recv():
turn off the recv callback
- stop_on_send():
turn off the send callback
which simply call
on_<evt>(None)
.The entire socket interface, excluding direct recv methods, is also provided, primarily through direct-linking the methods. e.g.
>>> stream.bind is stream.socket.bind True
New in version 25: send/recv callbacks can be coroutines.
Changed in version 25: ZMQStreams only support base zmq.Socket classes (this has always been true, but not enforced). If ZMQStreams are created with e.g. async Socket subclasses, a RuntimeWarning will be shown, and the socket cast back to the default zmq.Socket before connecting events.
Previously, using async sockets (or any zmq.Socket subclass) would result in undefined behavior for the arguments passed to callback functions. Now, the callback functions reliably get the return value of the base
zmq.Socket
send/recv_multipart methods (the list of message frames).- flush(flag: int = PollEvent.POLLIN | POLLOUT, limit: int | None = None)#
Flush pending messages.
This method safely handles all pending incoming and/or outgoing messages, bypassing the inner loop, passing them to the registered callbacks.
A limit can be specified, to prevent blocking under high load.
- flush will return the first time ANY of these conditions are met:
No more events matching the flag are pending.
the total number of events handled reaches the limit.
Note that if
flag|POLLIN != 0
, recv events will be flushed even if no callback is registered, unlike normal IOLoop operation. This allows flush to be used to remove and ignore incoming messages.- Parameters:
flag (int) – default=POLLIN|POLLOUT 0MQ poll flags. If flag|POLLIN, recv events will be flushed. If flag|POLLOUT, send events will be flushed. Both flags can be set at once, which is the default.
limit (None or int, optional) – The maximum number of messages to send or receive. Both send and recv count against this limit.
- Returns:
count of events handled (both send and recv)
- Return type:
- on_recv(callback: Callable[[list[bytes]], Any]) None #
- on_recv(callback: Callable[[list[bytes]], Any], copy: Literal[True]) None
- on_recv(callback: Callable[[list[Frame]], Any], copy: Literal[False]) None
- on_recv(callback: Callable[[list[Frame]], Any] | Callable[[list[bytes]], Any], copy: bool = True)
Register a callback for when a message is ready to recv.
There can be only one callback registered at a time, so each call to
on_recv
replaces previously registered callbacks.on_recv(None) disables recv event polling.
Use on_recv_stream(callback) instead, to register a callback that will receive both this ZMQStream and the message, instead of just the message.
- Parameters:
callback (callable) – callback must take exactly one argument, which will be a list, as returned by socket.recv_multipart() if callback is None, recv callbacks are disabled.
copy (bool) – copy is passed directly to recv, so if copy is False, callback will receive Message objects. If copy is True, then callback will receive bytes/str objects.
Returns (None) –
- on_recv_stream(callback: Callable[[ZMQStream, list[bytes]], Any]) None #
- on_recv_stream(callback: Callable[[ZMQStream, list[bytes]], Any], copy: Literal[True]) None
- on_recv_stream(callback: Callable[[ZMQStream, list[Frame]], Any], copy: Literal[False]) None
- on_recv_stream(callback: Callable[[ZMQStream, list[Frame]], Any] | Callable[[ZMQStream, list[bytes]], Any], copy: bool = True)
Same as on_recv, but callback will get this stream as first argument
callback must take exactly two arguments, as it will be called as:
callback(stream, msg)
Useful when a single callback should be used with multiple streams.
- on_send(callback: Callable[[Sequence[Any], MessageTracker | None], Any])#
Register a callback to be called on each send
There will be two arguments:
callback(msg, status)
msg
will be the list of sendable objects that was just sentstatus
will be the return result of socket.send_multipart(msg) - MessageTracker or None.
Non-copying sends return a MessageTracker object whose
done
attribute will be True when the send is complete. This allows users to track when an object is safe to write to again.The second argument will always be None if copy=True on the send.
Use on_send_stream(callback) to register a callback that will be passed this ZMQStream as the first argument, in addition to the other two.
on_send(None) disables recv event polling.
- Parameters:
callback (callable) –
callback must take exactly two arguments, which will be the message being sent (always a list), and the return result of socket.send_multipart(msg) - MessageTracker or None.
if callback is None, send callbacks are disabled.
- on_send_stream(callback: Callable[[ZMQStream, Sequence[Any], MessageTracker | None], Any])#
Same as on_send, but callback will get this stream as first argument
Callback will be passed three arguments:
callback(stream, msg, status)
Useful when a single callback should be used with multiple streams.
- send(msg, flags=0, copy=True, track=False, callback=None, **kwargs)#
Send a message, optionally also register a new callback for sends. See zmq.socket.send for details.
- send_json(obj: Any, flags: int = 0, callback: Callable | None = None, **kwargs: Any)#
Send json-serialized version of an object. See zmq.socket.send_json for details.
- send_multipart(msg: Sequence[Any], flags: int = 0, copy: bool = True, track: bool = False, callback: Callable | None = None, **kwargs: Any) None #
Send a multipart message, optionally also register a new callback for sends. See zmq.socket.send_multipart for details.
- send_pyobj(obj: Any, flags: int = 0, protocol: int = -1, callback: Callable | None = None, **kwargs: Any)#
Send a Python object as a message using pickle to serialize.
See zmq.socket.send_json for details.
- send_string(u: str, flags: int = 0, encoding: str = 'utf-8', callback: Callable | None = None, **kwargs: Any)#
Send a unicode message with an encoding. See zmq.socket.send_unicode for details.
- send_unicode(u: str, flags: int = 0, encoding: str = 'utf-8', callback: Callable | None = None, **kwargs: Any)#
Send a unicode message with an encoding. See zmq.socket.send_unicode for details.
- stop_on_err()#
DEPRECATED, does nothing
- stop_on_recv()#
Disable callback and automatic receiving.
- stop_on_send()#
Disable callback on sending.