asyncio

Module: zmq.asyncio

AsyncIO support for zmq

Requires asyncio and Python 3.

New in version 15.0.

As of 15.0, pyzmq now supports asyncio, via zmq.asyncio. When imported from this module, blocking methods such as zmq.asyncio.Socket.recv_multipart(), zmq.asyncio.Socket.poll(), and zmq.asyncio.Poller.poll() return Future s.

import asyncio
import zmq
import zmq.asyncio

ctx = zmq.asyncio.Context()

@asyncio.coroutine
def recv_and_process():
    sock = ctx.socket(zmq.PULL)
    sock.bind(url)
    msg = yield from sock.recv_multipart() # waits for msg to be ready
    reply = yield from async_process(msg)
    yield from sock.send_multipart(reply)

asyncio.get_event_loop().run_until_complete(recv_and_process())

Classes

ZMQEventLoop

class zmq.asyncio.ZMQEventLoop(selector=None)

DEPRECATED: AsyncIO eventloop using zmq_poll.

pyzmq sockets should work with any asyncio event loop as of pyzmq 17.

Context

Context class that creates Future-returning sockets. See zmq.Context for more info.

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

Context for creating asyncio-compatible Sockets

Socket

Socket subclass that returns asyncio.Future s from blocking methods, for use in coroutines and async applications.

See also

zmq.Socket for the inherited API.

class zmq.asyncio.Socket(context=None, socket_type=-1, io_loop=None, **kwargs)

Socket returning asyncio Futures for send/recv/poll methods.

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

Receive a single zmq frame.

Returns a Future, whose result will be the received frame.

Recommend using recv_multipart instead.

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

Receive a complete multipart zmq message.

Returns a Future whose result will be a multipart message.

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

Send a single zmq frame.

Returns a Future that resolves when sending is complete.

Recommend using send_multipart instead.

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

Send a complete multipart zmq message.

Returns a Future that resolves when sending is complete.

poll(timeout=None, flags=1)

poll the socket for events

returns a Future for the poll results.

Poller

Poller subclass that returns asyncio.Future s from poll, for use in coroutines and async applications.

See also

zmq.Poller for the inherited API.

class zmq.asyncio.Poller

Poller returning asyncio.Future for poll results.

poll(timeout=-1)

Return a Future for a poll event