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

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#

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: int = 1)
class zmq.asyncio.Context(io_threads: Context)
class zmq.asyncio.Context(*, shadow: Union[Context, int])

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: Optional[zmq.sugar.socket.Socket] = None, **kwargs)

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

recv(flags: int = 0, copy: bool = True, track: bool = False) Awaitable[Union[bytes, zmq.sugar.frame.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[Union[List[bytes], List[zmq.sugar.frame.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[Optional[zmq.sugar.tracker.MessageTracker]]

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[Optional[zmq.sugar.tracker.MessageTracker]]

Send a complete multipart zmq message.

Returns a Future that resolves when sending is complete.

poll(timeout=None, flags=PollEvent.POLLIN) 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