eventlet.green.zmq – ØMQ support

The zmq module wraps the Socket and Context found in pyzmq to be non blocking

eventlet.green.zmq.Context()

Subclass of zmq.core.context.Context

class eventlet.green.zmq.Socket(context, socket_type)

Bases: zmq.core.socket.Socket

Green version of :class:`zmq.core.socket.Socket

The following three methods are always overridden:
  • send
  • recv
  • getsockopt

To ensure that the zmq.NOBLOCK flag is set and that sending or recieving is deferred to the hub (using eventlet.hubs.trampoline()) if a zmq.EAGAIN (retry) error is raised

For some socket types, the following methods are also overridden:
  • send_multipart
  • recv_multipart
recv(flags=0, copy=True, track=False)

Receive a message.

flags : int
Any supported flag: NOBLOCK. If NOBLOCK is set, this method will raise a ZMQError with EAGAIN if a message is not ready. If NOBLOCK is not set, then this method will block until a message arrives.
copy : bool
Should the message be received in a copying or non-copying manner? If False a Message object is returned, if True a string copy of message is returned.
track : bool
Should the message be tracked for notification that ZMQ has finished with it? (ignored if copy=True)
msg : str, Message
The returned message. If copy is False, then it will be a Message, otherwise a str.
ZMQError
for any of the reasons zmq_recvmsg might fail.
send(data, flags=0, copy=True, track=False)

Send a message on this socket.

This queues the message to be sent by the IO thread at a later time.

data : object, str, Message
The content of the message.
flags : int
Any supported flag: NOBLOCK, SNDMORE.
copy : bool
Should the message be sent in a copying or non-copying manner.
track : bool
Should the message be tracked for notification that ZMQ has finished with it? (ignored if copy=True)
None : if copy or not track
None if message was sent, raises an exception otherwise.
MessageTracker : if track and not copy
a MessageTracker object, whose pending property will be True until the send is completed.
TypeError
If a unicode object is passed
ValueError
If track=True, but an untracked Message is passed.
ZMQError
If the send does not succeed for any reason.

zmq – The pyzmq ØMQ python bindings

pyzmq [1] Is a python binding to the C++ ØMQ [2] library written in Cython [3]. The following is auto generated pyzmq's from documentation.

class zmq.core.context.Context

Context(io_threads=1)

Manage the lifecycle of a 0MQ context.

io_threads : int
The number of IO threads.
destroy(linger=None)

Close all sockets associated with this context, and then terminate the context. If linger is specified, the LINGER sockopt of the sockets will be set prior to closing.

WARNING:

destroy involves calling zmq_close(), which is NOT threadsafe. If there are active sockets in other threads, this must not be called.

static instance()

Returns a global Context instance.

Most single-threaded applications have a single, global Context. Use this method instead of passing around Context instances throughout your code.

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

class MyClass(object):
def __init__(self, context=None):
self.context = context or Context.instance()
socket(socket_type)

Create a Socket associated with this Context.

socket_type : int
The socket type, which can be any of the 0MQ socket types: REQ, REP, PUB, SUB, PAIR, XREQ, DEALER, XREP, ROUTER, PULL, PUSH, XSUB, XPUB.
term()

Close or terminate the context.

This can be called to close the context by hand. If this is not called, the context will automatically be closed when it is garbage collected.

class zmq.core.socket.Socket

Socket(context, socket_type)

A 0MQ socket.

These objects will generally be constructed via the socket() method of a Context object.

Note: 0MQ Sockets are not threadsafe. DO NOT share them across threads.

context : Context
The 0MQ Context this Socket belongs to.
socket_type : int
The socket type, which can be any of the 0MQ socket types: REQ, REP, PUB, SUB, PAIR, XREQ, DEALER, XREP, ROUTER, PULL, PUSH, XPUB, XSUB.

.Context.socket : method for creating a socket bound to a Context.

class zmq.core.poll.Poller

Poller()

A stateful poll interface that mirrors Python’s built-in poll.

modify(socket, flags=POLLIN|POLLOUT)

Modify the flags for an already registered 0MQ socket or native fd.

poll(timeout=None)

Poll the registered 0MQ or native fds for I/O.

timeout : float, int
The timeout in milliseconds. If None, no timeout (infinite). This is in milliseconds to be compatible with select.poll(). The underlying zmq_poll uses microseconds and we convert to that in this function.
register(socket, flags=POLLIN|POLLOUT)

Register a 0MQ socket or native fd for I/O monitoring.

register(s,0) is equivalent to unregister(s).

socket : zmq.Socket or native socket
A zmq.Socket or any Python object having a fileno() method that returns a valid file descriptor.
flags : int
The events to watch for. Can be POLLIN, POLLOUT or POLLIN|POLLOUT. If flags=0, socket will be unregistered.
unregister(socket)

Remove a 0MQ socket or native fd for I/O monitoring.

socket : Socket
The socket instance to stop polling.
[1]http://github.com/zeromq/pyzmq
[2]http://www.zeromq.com
[3]http://www.cython.org

Table Of Contents

Previous topic

wsgi – WSGI server

Next topic

Authors

This Page