PyZMQ Documentation

Table Of Contents

Previous topic

green

Next topic

eventloop.zmqstream

This Page

eventloop.ioloop

Module: eventloop.ioloop

tornado IOLoop API with zmq compatibility

If you have tornado ≥ 3.0, this is a subclass of tornado’s IOLoop, otherwise we ship a minimal subset of tornado in zmq.eventloop.minitornado.

The minimal shipped version of tornado’s IOLoop does not include support for concurrent futures - this will only be available if you have tornado ≥ 3.0.

Classes

DelayedCallback

class zmq.eventloop.ioloop.DelayedCallback(callback, callback_time, io_loop=None)

Schedules the given callback to be called once.

The callback is called once, after callback_time milliseconds.

start must be called after the DelayedCallback is created.

The timeout is calculated from when start is called.

Methods

start()

Starts the timer.

stop()

Stops the timer.

ZMQIOLoop

class zmq.eventloop.ioloop.ZMQIOLoop

ZMQ subclass of tornado’s IOLoop

Methods

ERROR = 24
NONE = 0
READ = 1
WRITE = 4
add_callback(callback, *args, **kwargs)
add_callback_from_signal(callback, *args, **kwargs)
add_future(future, callback)

Schedules a callback on the IOLoop when the given .Future is finished.

The callback is invoked with one argument, the .Future.

add_handler(fd, handler, events)
add_timeout(deadline, callback)
static clear_current()
close(all_fds=False)
classmethod configurable_base()
classmethod configurable_default()
classmethod configure(impl, **kwargs)

Sets the class to use when the base class is instantiated.

Keyword arguments will be saved and added to the arguments passed to the constructor. This can be used to set global defaults for some parameters.

classmethod configured_class()

Returns the currently configured class.

static current()

Returns the current thread’s IOLoop.

If an IOLoop is currently running or has been marked as current by make_current, returns that instance. Otherwise returns IOLoop.instance(), i.e. the main thread’s IOLoop.

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

class MyClass(object):
    def __init__(self, io_loop=None):
        self.io_loop = io_loop or IOLoop.current()

In general you should use IOLoop.current as the default when constructing an asynchronous object, and use IOLoop.instance when you mean to communicate to the main thread from a different one.

handle_callback_exception(callback)

This method is called whenever a callback run by the IOLoop throws an exception.

By default simply logs the exception as an error. Subclasses may override this method to customize reporting of exceptions.

The exception itself is not passed explicitly, but is available in sys.exc_info.

initialize(impl=None, **kwargs)
static initialized()

Returns true if the singleton instance has been created.

install()

Installs this IOLoop object as the singleton instance.

This is normally not necessary as instance() will create an IOLoop on demand, but you may want to call install to use a custom subclass of IOLoop.

static instance()

Returns a global IOLoop instance.

Most applications have a single, global IOLoop running on the main thread. Use this method to get this instance from another thread. To get the current thread’s IOLoop, use current().

log_stack(signal, frame)

Signal handler to log the stack trace of the current thread.

For use with set_blocking_signal_threshold.

make_current()

Makes this the IOLoop for the current thread.

An IOLoop automatically becomes current for its thread when it is started, but it is sometimes useful to call make_current explictly before starting the IOLoop, so that code run at startup time can find the right instance.

remove_handler(fd)
remove_timeout(timeout)
run_sync(func, timeout=None)

Starts the IOLoop, runs the given function, and stops the loop.

If the function returns a .Future, the IOLoop will run until the future is resolved. If it raises an exception, the IOLoop will stop and the exception will be re-raised to the caller.

The keyword-only argument timeout may be used to set a maximum duration for the function. If the timeout expires, a TimeoutError is raised.

This method is useful in conjunction with tornado.gen.coroutine to allow asynchronous calls in a main() function:

@gen.coroutine
def main():
    # do stuff...

if __name__ == '__main__':
    IOLoop.instance().run_sync(main)
set_blocking_log_threshold(seconds)

Logs a stack trace if the IOLoop is blocked for more than s seconds.

Equivalent to set_blocking_signal_threshold(seconds, self.log_stack)

set_blocking_signal_threshold(seconds, action)
start()
stop()
time()
update_handler(fd, events)

ZMQPoller

class zmq.eventloop.ioloop.ZMQPoller

A poller that can be used in the tornado IOLoop.

This simply wraps a regular zmq.Poller, scaling the timeout by 1000, so that it is in seconds rather than milliseconds.

Methods

close()
modify(fd, events)
poll(timeout)

poll in seconds rather than milliseconds.

Event masks will be IOLoop.READ/WRITE/ERROR

register(fd, events)
unregister(fd)

Function

zmq.eventloop.ioloop.install()

set the tornado IOLoop instance with the pyzmq IOLoop.

After calling this function, tornado’s IOLoop.instance() and pyzmq’s IOLoop.instance() will return the same object.

An assertion error will be raised if tornado’s IOLoop has been initialized prior to calling this function.