PyZMQ Documentation

Table Of Contents

Previous topic

eventloop.ioloop

Next topic

auth

This Page

eventloop.zmqstream

Module: eventloop.zmqstream

A utility class to send to and recv from a non-blocking socket.

ZMQStream

class zmq.eventloop.zmqstream.ZMQStream(socket, io_loop=None)

A utility class to register callbacks when a zmq socket sends and receives

For use with zmq.eventloop.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(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
close()

Close this stream.

closed()
flush(flag=3, limit=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:

int : count of events handled (both send and recv)

io_loop = None
on_err(callback)

DEPRECATED, does nothing

on_recv(callback, copy=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, copy=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)

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 sent
  • status 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)

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.

poller = None
receiving()

Returns True if we are currently receiving from the stream.

send(msg, flags=0, copy=True, track=False, callback=None)

Send a message, optionally also register a new callback for sends. See zmq.socket.send for details.

send_json(obj, flags=0, callback=None)

Send json-serialized version of an object. See zmq.socket.send_json for details.

send_multipart(msg, flags=0, copy=True, track=False, callback=None)

Send a multipart message, optionally also register a new callback for sends. See zmq.socket.send_multipart for details.

send_pyobj(obj, flags=0, protocol=-1, callback=None)

Send a Python object as a message using pickle to serialize.

See zmq.socket.send_json for details.

send_string(u, flags=0, encoding='utf-8', callback=None)

Send a unicode message with an encoding. See zmq.socket.send_unicode for details.

send_unicode(u, flags=0, encoding='utf-8', callback=None)

Send a unicode message with an encoding. See zmq.socket.send_unicode for details.

sending()

Returns True if we are currently sending to the stream.

set_close_callback(callback)

Call the given callback when the stream is closed.

socket = None
stop_on_err()

DEPRECATED, does nothing

stop_on_recv()

Disable callback and automatic receiving.

stop_on_send()

Disable callback on sending.