PyZMQ Documentation

Table Of Contents

Previous topic

eventloop.stack_context

Next topic

log.handlers

This Page

eventloop.zmqstream

Module: eventloop.zmqstream

Inheritance diagram for zmq.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)

Bases: object

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

For use with zmq.eventloop.ioloop

There are 4 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

  • on_err(callback):

    register a callback to be run every time there is an error

  • 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

  • stop_on_err():

    turn off the error callback

All of 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)

on_err(callback)

register a callback to be called on POLLERR events with no arguments.

Parameters :

callback : callable

callback will be passed no arguments.

on_recv(callback, copy=True)

Register a callback to be called 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.

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_send(callback)

Register a callback to be called on each send There will be two arguments: the message being sent (always a list), and the return result of socket.send_multipart(msg).

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.

on_send(None) disables recv event polling.

Parameters :

callback : callable

callback must take exactly two arguments, which will be There will be two arguments: 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.

receiving()

Returns True if we are currently receiving from the stream.

send(msg, flags=0, copy=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=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_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.

stop_on_err()

Disable callback on errors.

stop_on_recv()

Disable callback and automatic receiving.

stop_on_send()

Disable callback on sending.