zmq

Python bindings for 0MQ.

Basic Classes

Context

class zmq.Context(io_threads=1, **kwargs)

Create a zmq Context

A zmq Context creates sockets via its ctx.socket method.

Attributes

sockopts  

Methods

closed

boolean - whether the context has been terminated. If True, you can no longer use this Context.

destroy(linger=None)

Close all sockets associated with this context, and then terminate the context. If linger is specified, the LINGER sockopt of the sockets will be set prior to closing.

Warning

destroy involves calling zmq_close(), which is NOT threadsafe. If there are active sockets in other threads, this must not be called.

get(option)

Get the value of a context option.

See the 0MQ API documentation for zmq_ctx_get for details on specific options.

New in version libzmq-3.2.

New in version 13.0.

Parameters:

option : int

The option to get. Available values will depend on your version of libzmq. Examples include:

zmq.IO_THREADS, zmq.MAX_SOCKETS
Returns:

optval : int

The value of the option as an integer.

getsockopt(opt)

get default socket options for new sockets created by this Context

New in version 13.0.

classmethod instance(io_threads=1)

Returns a global Context instance.

Most single-threaded applications have a single, global Context. Use this method instead of passing around Context instances throughout your code.

A common pattern for classes that depend on Contexts is to use a default argument to enable programs with multiple Contexts but not require the argument for simpler applications:

class MyClass(object):
def __init__(self, context=None):
self.context = context or Context.instance()
set(option, optval)

Set a context option.

See the 0MQ API documentation for zmq_ctx_set for details on specific options.

New in version libzmq-3.2.

New in version 13.0.

Parameters:

option : int

The option to set. Available values will depend on your version of libzmq. Examples include:

zmq.IO_THREADS, zmq.MAX_SOCKETS

optval : int

The value of the option to set.

setsockopt(opt, value)

set default socket options for new sockets created by this Context

New in version 13.0.

classmethod shadow(address)

Shadow an existing libzmq context

address is the integer address of the libzmq context or an FFI pointer to it.

New in version 14.1.

classmethod shadow_pyczmq(ctx)

Shadow an existing pyczmq context

ctx is the FFI zctx_t * pointer

New in version 14.1.

socket(socket_type)

Create a Socket associated with this Context.

Parameters:

socket_type : int

The socket type, which can be any of the 0MQ socket types: REQ, REP, PUB, SUB, PAIR, DEALER, ROUTER, PULL, PUSH, etc.

term()

Close or terminate the context.

This can be called to close the context by hand. If this is not called, the context will automatically be closed when it is garbage collected.

underlying

The address of the underlying libzmq context

Socket

class zmq.Socket(*a, **kw)

The ZMQ socket object

To create a Socket, first create a Context:

ctx = zmq.Context.instance()

then call ctx.socket(socket_type):

s = ctx.socket(zmq.ROUTER)

Attributes

Methods

closed

boolean - whether the socket has been closed. If True, you can no longer use this Socket.

bind(addr)

Bind the socket to an address.

This causes the socket to listen on a network port. Sockets on the other side of this connection will use Socket.connect(addr) to connect to this socket.

Parameters:

addr : str

The address string. This has the form ‘protocol://interface:port’, for example ‘tcp://127.0.0.1:5555’. Protocols supported include tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is encoded to utf-8 first.

bind_to_random_port(addr, min_port=49152, max_port=65536, max_tries=100)

bind this socket to a random port in a range

If the port range is unspecified, the system will choose the port.

Parameters:

addr : str

The address string without the port to pass to Socket.bind().

min_port : int, optional

The minimum port in the range of ports to try (inclusive).

max_port : int, optional

The maximum port in the range of ports to try (exclusive).

max_tries : int, optional

The maximum number of bind attempts to make.

Returns:

port : int

The port the socket was bound to.

Raises:

ZMQBindError

if max_tries reached before successful bind

close(linger=None)

Close the socket.

If linger is specified, LINGER sockopt will be set prior to closing.

This can be called to close the socket by hand. If this is not called, the socket will automatically be closed when it is garbage collected.

connect(addr)

Connect to a remote 0MQ socket.

Parameters:

addr : str

The address string. This has the form ‘protocol://interface:port’, for example ‘tcp://127.0.0.1:5555’. Protocols supported are tcp, upd, pgm, inproc and ipc. If the address is unicode, it is encoded to utf-8 first.

disable_monitor()

Shutdown the PAIR socket (created using get_monitor_socket) that is serving socket events.

New in version 14.4.

disconnect(addr)

Disconnect from a remote 0MQ socket (undoes a call to connect).

New in version libzmq-3.2.

New in version 13.0.

Parameters:

addr : str

The address string. This has the form ‘protocol://interface:port’, for example ‘tcp://127.0.0.1:5555’. Protocols supported are tcp, upd, pgm, inproc and ipc. If the address is unicode, it is encoded to utf-8 first.

get(option)

Get the value of a socket option.

See the 0MQ API documentation for details on specific options.

Parameters:

option : int

The option to get. Available values will depend on your version of libzmq. Examples include:

zmq.IDENTITY, HWM, LINGER, FD, EVENTS
Returns:

optval : int or bytes

The value of the option as a bytestring or int.

get_hwm()

get the High Water Mark

On libzmq ≥ 3, this gets SNDHWM if available, otherwise RCVHWM

get_monitor_socket(events=None, addr=None)

Return a connected PAIR socket ready to receive the event notifications.

New in version libzmq-4.0.

New in version 14.0.

Parameters:

events : bitfield (int) [default: ZMQ_EVENTS_ALL]

The bitmask defining which events are wanted.

addr : string [default: None]

The optional endpoint for the monitoring sockets.

Returns:

socket : (PAIR)

The socket is already connected and ready to receive messages.

get_string(option, encoding='utf-8')

get the value of a socket option

See the 0MQ documentation for details on specific options.

Parameters:

option : int

The option to retrieve.

Returns:

optval : unicode string (unicode on py2, str on py3)

The value of the option as a unicode string.

getsockopt(option)

Get the value of a socket option.

See the 0MQ API documentation for details on specific options.

Parameters:

option : int

The option to get. Available values will depend on your version of libzmq. Examples include:

zmq.IDENTITY, HWM, LINGER, FD, EVENTS
Returns:

optval : int or bytes

The value of the option as a bytestring or int.

getsockopt_string(option, encoding='utf-8')

get the value of a socket option

See the 0MQ documentation for details on specific options.

Parameters:

option : int

The option to retrieve.

Returns:

optval : unicode string (unicode on py2, str on py3)

The value of the option as a unicode string.

hwm

get the High Water Mark

On libzmq ≥ 3, this gets SNDHWM if available, otherwise RCVHWM

monitor(addr, flags)

Start publishing socket events on inproc. See libzmq docs for zmq_monitor for details.

While this function is available from libzmq 3.2, pyzmq cannot parse monitor messages from libzmq prior to 4.0.

Parameters:

addr : str

The inproc url used for monitoring. Passing None as the addr will cause an existing socket monitor to be deregistered.

events : int [default: zmq.EVENT_ALL]

The zmq event bitmask for which events will be sent to the monitor.

poll(timeout=None, flags=1)

poll the socket for events

The default is to poll forever for incoming events. Timeout is in milliseconds, if specified.

Parameters:

timeout : int [default: None]

The timeout (in milliseconds) to wait for an event. If unspecified (or specified None), will wait forever for an event.

flags : bitfield (int) [default: POLLIN]

The event flags to poll for (any combination of POLLIN|POLLOUT). The default is to check for incoming events (POLLIN).

Returns:

events : bitfield (int)

The events that are ready and waiting. Will be 0 if no events were ready by the time timeout was reached.

recv(flags=0, copy=True, track=False)

Receive a message.

Parameters:

flags : int

Any supported flag: NOBLOCK. If NOBLOCK is set, this method will raise a ZMQError with EAGAIN if a message is not ready. If NOBLOCK is not set, then this method will block until a message arrives.

copy : bool

Should the message be received in a copying or non-copying manner? If False a Frame object is returned, if True a string copy of message is returned.

track : bool

Should the message be tracked for notification that ZMQ has finished with it? (ignored if copy=True)

Returns:

msg : bytes, Frame

The received message frame. If copy is False, then it will be a Frame, otherwise it will be bytes.

Raises:

ZMQError

for any of the reasons zmq_msg_recv might fail.

recv_json(flags=0, **kwargs)

receive a Python object as a message using json to serialize

Keyword arguments are passed on to json.loads

Parameters:

flags : int

Any valid recv flag.

Returns:

obj : Python object

The Python object that arrives as a message.

recv_multipart(flags=0, copy=True, track=False)

receive a multipart message as a list of bytes or Frame objects

Parameters:

flags : int, optional

Any supported flag: NOBLOCK. If NOBLOCK is set, this method will raise a ZMQError with EAGAIN if a message is not ready. If NOBLOCK is not set, then this method will block until a message arrives.

copy : bool, optional

Should the message frame(s) be received in a copying or non-copying manner? If False a Frame object is returned for each part, if True a copy of the bytes is made for each frame.

track : bool, optional

Should the message frame(s) be tracked for notification that ZMQ has finished with it? (ignored if copy=True)

Returns:

msg_parts : list

A list of frames in the multipart message; either Frames or bytes, depending on copy.

recv_pyobj(flags=0)

receive a Python object as a message using pickle to serialize

Parameters:

flags : int

Any valid recv flag.

Returns:

obj : Python object

The Python object that arrives as a message.

recv_string(flags=0, encoding='utf-8')

receive a unicode string, as sent by send_string

Parameters:

flags : int

Any valid recv flag.

encoding : str [default: ‘utf-8’]

The encoding to be used

Returns:

s : unicode string (unicode on py2, str on py3)

The Python unicode string that arrives as encoded bytes.

send(data, flags=0, copy=True, track=False)

Send a message on this socket.

This queues the message to be sent by the IO thread at a later time.

Parameters:

data : object, str, Frame

The content of the message.

flags : int

Any supported flag: NOBLOCK, SNDMORE.

copy : bool

Should the message be sent in a copying or non-copying manner.

track : bool

Should the message be tracked for notification that ZMQ has finished with it? (ignored if copy=True)

Returns:

None : if copy or not track

None if message was sent, raises an exception otherwise.

MessageTracker : if track and not copy

a MessageTracker object, whose pending property will be True until the send is completed.

Raises:

TypeError

If a unicode object is passed

ValueError

If track=True, but an untracked Frame is passed.

ZMQError

If the send does not succeed for any reason.

send_json(obj, flags=0, **kwargs)

send a Python object as a message using json to serialize

Keyword arguments are passed on to json.dumps

Parameters:

obj : Python object

The Python object to send

flags : int

Any valid send flag

send_multipart(msg_parts, flags=0, copy=True, track=False)

send a sequence of buffers as a multipart message

The zmq.SNDMORE flag is added to all msg parts before the last.

Parameters:

msg_parts : iterable

A sequence of objects to send as a multipart message. Each element can be any sendable object (Frame, bytes, buffer-providers)

flags : int, optional

SNDMORE is handled automatically for frames before the last.

copy : bool, optional

Should the frame(s) be sent in a copying or non-copying manner.

track : bool, optional

Should the frame(s) be tracked for notification that ZMQ has finished with it (ignored if copy=True).

Returns:

None : if copy or not track

MessageTracker : if track and not copy

a MessageTracker object, whose pending property will be True until the last send is completed.

send_pyobj(obj, flags=0, protocol=2)

send a Python object as a message using pickle to serialize

Parameters:

obj : Python object

The Python object to send.

flags : int

Any valid send flag.

protocol : int

The pickle protocol number to use. The default is pickle.DEFAULT_PROTOCOL where defined, and pickle.HIGHEST_PROTOCOL elsewhere.

send_string(u, flags=0, copy=True, encoding='utf-8')

send a Python unicode string as a message with an encoding

0MQ communicates with raw bytes, so you must encode/decode text (unicode on py2, str on py3) around 0MQ.

Parameters:

u : Python unicode string (unicode on py2, str on py3)

The unicode string to send.

flags : int, optional

Any valid send flag.

encoding : str [default: ‘utf-8’]

The encoding to be used

set(option, optval)

Set socket options.

See the 0MQ API documentation for details on specific options.

Parameters:

option : int

The option to set. Available values will depend on your version of libzmq. Examples include:

zmq.SUBSCRIBE, UNSUBSCRIBE, IDENTITY, HWM, LINGER, FD

optval : int or bytes

The value of the option to set.

Notes

Warning

All options other than zmq.SUBSCRIBE, zmq.UNSUBSCRIBE and zmq.LINGER only take effect for subsequent socket bind/connects.

set_hwm(value)

set the High Water Mark

On libzmq ≥ 3, this sets both SNDHWM and RCVHWM

Warning

New values only take effect for subsequent socket bind/connects.

set_string(option, optval, encoding='utf-8')

set socket options with a unicode object

This is simply a wrapper for setsockopt to protect from encoding ambiguity.

See the 0MQ documentation for details on specific options.

Parameters:

option : int

The name of the option to set. Can be any of: SUBSCRIBE, UNSUBSCRIBE, IDENTITY

optval : unicode string (unicode on py2, str on py3)

The value of the option to set.

encoding : str

The encoding to be used, default is utf8

setsockopt(option, optval)

Set socket options.

See the 0MQ API documentation for details on specific options.

Parameters:

option : int

The option to set. Available values will depend on your version of libzmq. Examples include:

zmq.SUBSCRIBE, UNSUBSCRIBE, IDENTITY, HWM, LINGER, FD

optval : int or bytes

The value of the option to set.

Notes

Warning

All options other than zmq.SUBSCRIBE, zmq.UNSUBSCRIBE and zmq.LINGER only take effect for subsequent socket bind/connects.

setsockopt_string(option, optval, encoding='utf-8')

set socket options with a unicode object

This is simply a wrapper for setsockopt to protect from encoding ambiguity.

See the 0MQ documentation for details on specific options.

Parameters:

option : int

The name of the option to set. Can be any of: SUBSCRIBE, UNSUBSCRIBE, IDENTITY

optval : unicode string (unicode on py2, str on py3)

The value of the option to set.

encoding : str

The encoding to be used, default is utf8

classmethod shadow(address)

Shadow an existing libzmq socket

address is the integer address of the libzmq socket or an FFI pointer to it.

New in version 14.1.

socket_type
unbind(addr)

Unbind from an address (undoes a call to bind).

New in version libzmq-3.2.

New in version 13.0.

Parameters:

addr : str

The address string. This has the form ‘protocol://interface:port’, for example ‘tcp://127.0.0.1:5555’. Protocols supported are tcp, upd, pgm, inproc and ipc. If the address is unicode, it is encoded to utf-8 first.

underlying

The address of the underlying libzmq socket

Frame

class zmq.Frame

Attributes

Methods

buffer

A read-only buffer view of the message contents.

bytes

The message content as a Python bytes object.

The first time this property is accessed, a copy of the message contents is made. From then on that same copy of the message is returned.

get(option)

Get a Frame option or property.

See the 0MQ API documentation for zmq_msg_get and zmq_msg_gets for details on specific options.

New in version libzmq-3.2.

New in version 13.0.

Changed in version 14.3: add support for zmq_msg_gets (requires libzmq-4.1)

set(option, value)

Set a Frame option.

See the 0MQ API documentation for zmq_msg_set for details on specific options.

New in version libzmq-3.2.

New in version 13.0.

MessageTracker

class zmq.MessageTracker(*towatch)

A class for tracking if 0MQ is done using one or more messages.

When you send a 0MQ message, it is not sent immediately. The 0MQ IO thread sends the message at some later time. Often you want to know when 0MQ has actually sent the message though. This is complicated by the fact that a single 0MQ message can be sent multiple times using different sockets. This class allows you to track all of the 0MQ usages of a message.

Parameters:

*towatch : tuple of Event, MessageTracker, Message instances.

This list of objects to track. This class can track the low-level Events used by the Message class, other MessageTrackers or actual Messages.

Attributes

events  
peers  

Methods

done

Is 0MQ completely done with the message(s) being tracked?

wait(timeout=-1)

Wait for 0MQ to be done with the message or until timeout.

Parameters:

timeout : float [default: -1, wait forever]

Maximum time in (s) to wait before raising NotDone.

Returns:

None

if done before timeout

Raises:

NotDone

if timeout reached before I am done.

Polling

Poller

class zmq.Poller

A stateful poll interface that mirrors Python’s built-in poll.

Attributes

sockets  

Methods

modify(socket, flags=3)

Modify the flags for an already registered 0MQ socket or native fd.

poll(timeout=None)

Poll the registered 0MQ or native fds for I/O.

Parameters:

timeout : float, int

The timeout in milliseconds. If None, no timeout (infinite). This is in milliseconds to be compatible with select.poll(). The underlying zmq_poll uses microseconds and we convert to that in this function.

Returns:

events : list of tuples

The list of events that are ready to be processed. This is a list of tuples of the form (socket, event), where the 0MQ Socket or integer fd is the first element, and the poll event mask (POLLIN, POLLOUT) is the second. It is common to call events = dict(poller.poll()), which turns the list of tuples into a mapping of socket : event.

register(socket, flags=POLLIN|POLLOUT)

Register a 0MQ socket or native fd for I/O monitoring.

register(s,0) is equivalent to unregister(s).

Parameters:

socket : zmq.Socket or native socket

A zmq.Socket or any Python object having a fileno() method that returns a valid file descriptor.

flags : int

The events to watch for. Can be POLLIN, POLLOUT or POLLIN|POLLOUT. If flags=0, socket will be unregistered.

unregister(socket)

Remove a 0MQ socket or native fd for I/O monitoring.

Parameters:

socket : Socket

The socket instance to stop polling.

zmq.select(rlist, wlist, xlist, timeout=None) -> (rlist, wlist, xlist)

Return the result of poll as a lists of sockets ready for r/w/exception.

This has the same interface as Python’s built-in select.select() function.

Parameters:

timeout : float, int, optional

The timeout in seconds. If None, no timeout (infinite). This is in seconds to be compatible with select.select(). The underlying zmq_poll uses microseconds and we convert to that in this function.

rlist : list of sockets/FDs

sockets/FDs to be polled for read events

wlist : list of sockets/FDs

sockets/FDs to be polled for write events

xlist : list of sockets/FDs

sockets/FDs to be polled for error events

Returns:

(rlist, wlist, xlist) : tuple of lists of sockets (length 3)

Lists correspond to sockets available for read/write/error events respectively.

Exceptions

ZMQError

class zmq.ZMQError(errno=None, msg=None)

Wrap an errno style error.

Parameters:

errno : int

The ZMQ errno or None. If None, then zmq_errno() is called and used.

msg : string

Description of the error or None.

Attributes

errno  

ZMQVersionError

class zmq.ZMQVersionError(min_version, msg='Feature')

Raised when a feature is not provided by the linked version of libzmq.

New in version 14.2.

Attributes

min_version  

Again

class zmq.Again(errno=None, msg=None)

Wrapper for zmq.EAGAIN

New in version 13.0.

Attributes

errno  

ContextTerminated

class zmq.ContextTerminated(errno=None, msg=None)

Wrapper for zmq.ETERM

New in version 13.0.

Attributes

errno  

NotDone

class zmq.NotDone

Raised when timeout is reached while waiting for 0MQ to finish with a Message

See also

MessageTracker.wait
object for tracking when ZeroMQ is done

Attributes

ZMQBindError

class zmq.ZMQBindError

An error for Socket.bind_to_random_port().

Attributes

Functions

zmq.zmq_version()

return the version of libzmq as a string

zmq.pyzmq_version()

return the version of pyzmq as a string

zmq.zmq_version_info()

Return the version of ZeroMQ itself as a 3-tuple of ints.

zmq.pyzmq_version_info()

return the pyzmq version as a tuple of at least three numbers

If pyzmq is a development version, inf will be appended after the third integer.

zmq.has()

Check for zmq capability by name (e.g. ‘ipc’, ‘curve’)

New in version libzmq-4.1.

New in version 14.1.

zmq.device(device_type, frontend, backend)

Start a zeromq device.

Deprecated since version libzmq-3.2: Use zmq.proxy

Parameters:

device_type : (QUEUE, FORWARDER, STREAMER)

The type of device to start.

frontend : Socket

The Socket instance for the incoming traffic.

backend : Socket

The Socket instance for the outbound traffic.

zmq.proxy(frontend, backend, capture)

Start a zeromq proxy (replacement for device).

New in version libzmq-3.2.

New in version 13.0.

Parameters:

frontend : Socket

The Socket instance for the incoming traffic.

backend : Socket

The Socket instance for the outbound traffic.

capture : Socket (optional)

The Socket instance for capturing traffic.

zmq.curve_keypair()

generate a Z85 keypair for use with zmq.CURVE security

Requires libzmq (≥ 4.0) to have been linked with libsodium.

New in version libzmq-4.0.

New in version 14.0.

Returns:

(public, secret) : two bytestrings

The public and private keypair as 40 byte z85-encoded bytestrings.

zmq.get_includes()

Return a list of directories to include for linking against pyzmq with cython.