eventloop.future

Module: eventloop.future

Future-returning APIs for coroutines.

New in version 15.0.

As of pyzmq 15, there is a new Socket subclass that returns Futures for recv methods, which can be found at zmq.eventloop.future.Socket. You can create these sockets by instantiating a Context from the same module. These sockets let you easily use zmq with tornado’s coroutines.

See also

tornado:tornado.gen

from tornado import gen
from zmq.eventloop.future import Context

ctx = Context()

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

Classes

Context

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

class zmq.eventloop.future.Context(*args, **kwargs)

Socket

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

See also

zmq.Socket for the inherited API.

class zmq.eventloop.future.Socket(context, socket_type, io_loop=None)
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 Future s from poll, for use in coroutines and async applications.

See also

zmq.Poller for the inherited API.

class zmq.eventloop.future.Poller
poll(timeout=-1)

Return a Future for a poll event