asyncio#

Module: zmq.asyncio#

AsyncIO support for zmq

Requires asyncio and Python 3.

Added in version 15.0.

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

import asyncio
import zmq
import zmq.asyncio

ctx = zmq.asyncio.Context()


async def recv_and_process():
    sock = ctx.socket(zmq.PULL)
    sock.bind(url)
    msg = await sock.recv_multipart()  # waits for msg to be ready
    reply = await async_process(msg)
    await sock.send_multipart(reply)


asyncio.run(recv_and_process())

Classes#

Context#

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

class zmq.asyncio.Context(io_threads: int | Context = 1, shadow: Context | int = 0)#

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, _from_socket: Socket | None = None, **kwargs)#

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

recv(flags: int = 0, copy: bool = True, track: bool = False) Awaitable[bytes | Frame]#

Receive a single zmq frame.

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

Recommend using recv_multipart instead.

recv_multipart(flags: int = 0, copy: bool = True, track: bool = False) Awaitable[list[bytes] | list[Frame]]#

Receive a complete multipart zmq message.

Returns a Future whose result will be a multipart message.

send(data: Any, flags: int = 0, copy: bool = True, track: bool = False, **kwargs: Any) Awaitable[MessageTracker | None]#

Send a single zmq frame.

Returns a Future that resolves when sending is complete.

Recommend using send_multipart instead.

send_multipart(msg_parts: Any, flags: int = 0, copy: bool = True, track=False, **kwargs) Awaitable[MessageTracker | None]#

Send a complete multipart zmq message.

Returns a Future that resolves when sending is complete.

poll(timeout=None, flags=<PollEvent.POLLIN: 1>) Awaitable[int]#

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) Awaitable[list[tuple[Any, int]]]#

Return a Future for a poll event