The zmq module wraps the Socket and Context found in pyzmq to be non blocking
-
eventlet.green.zmq.Context(io_threads=1)
Factory function replacement for zmq.core.context.Context
This factory ensures the zeromq hub
is the active hub, and defers creation (or retreival) of the Context
to the hub’s get_context() method
It’s a factory function due to the fact that there can only be one _Context
instance per thread. This is due to the way zmq.core.poll.Poller
works
-
class eventlet.green.zmq._Context
Bases: zmq.core.context.Context
Internal subclass of zmq.core.context.Context
-
socket(socket_type)
Overridden method to ensure that the green version of socket is used
Behaves the same as zmq.core.context.Context.socket(), but ensures
that a Socket with all of its send and recv methods set to be
non-blocking is returned
-
class eventlet.green.zmq.Socket
Bases: zmq.core.socket.Socket
Green version of :class:`zmq.core.socket.Socket
The following four methods are overridden:
- _send_message
- _send_copy
- _recv_message
- _recv_copy
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
-
recv(flags=0, copy=True, track=False)
Override this instead of the internal _recv_* methods
since those change and it’s not clear when/how they’re
called in real code.
-
send(msg, flags=0, copy=True, track=False)
Override this instead of the internal _send_* methods
since those change and it’s not clear when/how they’re
called in real code.
-
bind(addr)
Bind the socket to an address.
This causes the socket to listen on a network port. Sockets on the
other side of this connection will use Socket.connect(addr) to
connect to this socket.
- addr : str
- The address string. This has the form ‘protocol://interface:port’,
for example ‘tcp://127.0.0.1:5555’. Protocols supported include
tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is
encoded to utf-8 first.
-
bind_to_random_port(addr, min_port=49152, max_port=65536, max_tries=100)
Bind this socket to a random port in a range.
- addr : str
- The address string without the port to pass to Socket.bind().
- min_port : int, optional
- The minimum port in the range of ports to try (inclusive).
- max_port : int, optional
- The maximum port in the range of ports to try (exclusive).
- max_tries : int, optional
- The maximum number of bind attempts to make.
- port : int
- The port the socket was bound to.
- ZMQBindError
- if max_tries reached before successful bind
-
close(linger=None)
Close the socket.
If linger is specified, LINGER sockopt will be set prior to closing.
This can be called to close the socket by hand. If this is not
called, the socket will automatically be closed when it is
garbage collected.
-
connect(addr)
Connect to a remote 0MQ socket.
- addr : str
- The address string. This has the form ‘protocol://interface:port’,
for example ‘tcp://127.0.0.1:5555’. Protocols supported are
tcp, upd, pgm, inproc and ipc. If the address is unicode, it is
encoded to utf-8 first.
-
getsockopt(option)
Get the value of a socket option.
See the 0MQ API documentation for details on specific options.
- option : int
The option to get. Available values will depend on your
version of libzmq. Examples include:
zmq.IDENTITY, HWM, LINGER, FD, EVENTS
- optval : int or bytes
- The value of the option as a bytestring or int.
-
getsockopt_string(option, encoding='utf-8')
Get the value of a socket option.
See the 0MQ documentation for details on specific options.
- option : int
- The option to retrieve. Currently, IDENTITY is the only
gettable option that can return a string.
- optval : unicode string (unicode on py2, str on py3)
- The value of the option as a unicode string.
-
getsockopt_unicode(option, encoding='utf-8')
s.getsockopt_string(option, encoding=’utf-8’)
Get the value of a socket option.
See the 0MQ documentation for details on specific options.
- option : int
- The option to retrieve. Currently, IDENTITY is the only
gettable option that can return a string.
- optval : unicode string (unicode on py2, str on py3)
- The value of the option as a unicode string.
-
poll(timeout=None, flags=POLLIN)
Poll the socket for events. The default is to poll forever for incoming
events. Timeout is in milliseconds, if specified.
- timeout : int [default: None]
- The timeout (in milliseconds) to wait for an event. If unspecified
(or secified None), will wait forever for an event.
- flags : bitfield (int) [default: POLLIN]
- The event flags to poll for (any combination of POLLIN|POLLOUT).
The default is to check for incoming events (POLLIN).
- events : bitfield (int)
- The events that are ready and waiting. Will be 0 if no events were ready
by the time timeout was reached.
-
recv(flags=0, copy=True, track=False)
Override this instead of the internal _recv_* methods
since those change and it’s not clear when/how they’re
called in real code.
-
recv_json(flags=0)
Receive a Python object as a message using json to serialize.
- flags : int
- Any valid recv flag.
- obj : Python object
- The Python object that arrives as a message.
-
recv_multipart(flags=0, copy=True, track=False)
Receive a multipart message as a list of bytes or Frame objects.
- flags : int, optional
- 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, optional
- Should the message frame(s) be received in a copying or non-copying manner?
If False a Frame object is returned for each part, if True a copy of
the bytes is made for each frame.
- track : bool, optional
- Should the message frame(s) be tracked for notification that ZMQ has
finished with it? (ignored if copy=True)
- msg_parts : list
- A list of frames in the multipart message; either Frames or bytes,
depending on copy.
-
recv_pyobj(flags=0)
Receive a Python object as a message using pickle to serialize.
- flags : int
- Any valid recv flag.
- obj : Python object
- The Python object that arrives as a message.
-
recv_string(flags=0, encoding='utf-8')
Receive a unicode string, as sent by send_string.
- flags : int
- Any valid recv flag.
- encoding : str [default: ‘utf-8’]
- The encoding to be used
- s : unicode string (unicode on py2, str on py3)
- The Python unicode string that arrives as encoded bytes.
-
recv_unicode(flags=0, encoding='utf-8')
s.recv_string(flags=0, encoding=’utf-8’)
Receive a unicode string, as sent by send_string.
- flags : int
- Any valid recv flag.
- encoding : str [default: ‘utf-8’]
- The encoding to be used
- s : unicode string (unicode on py2, str on py3)
- The Python unicode string that arrives as encoded bytes.
-
send(msg, flags=0, copy=True, track=False)
Override this instead of the internal _send_* methods
since those change and it’s not clear when/how they’re
called in real code.
-
send_json(obj, flags=0)
Send a Python object as a message using json to serialize.
- obj : Python object
- The Python object to send.
- flags : int
- Any valid send flag.
-
send_multipart(msg_parts, flags=0, copy=True, track=False)
Send a sequence of buffers as a multipart message.
- msg_parts : iterable
- A sequence of objects to send as a multipart message. Each element
can be any sendable object (Frame, bytes, buffer-providers)
- flags : int, optional
- SNDMORE is handled automatically for frames before the last.
- copy : bool, optional
- Should the frame(s) be sent in a copying or non-copying manner.
- track : bool, optional
- Should the frame(s) be tracked for notification that ZMQ has
finished with it (ignored if copy=True).
None : if copy or not track
MessageTracker : if track and not copy
a MessageTracker object, whose pending property will
be True until the last send is completed.
-
send_pyobj(obj, flags=0, protocol=-1)
Send a Python object as a message using pickle to serialize.
- obj : Python object
- The Python object to send.
- flags : int
- Any valid send flag.
- protocol : int
- The pickle protocol number to use. Default of -1 will select
the highest supported number. Use 0 for multiple platform
support.
-
send_string(u, flags=0, copy=False, encoding='utf-8')
Send a Python unicode string as a message with an encoding.
0MQ communicates with raw bytes, so you must encode/decode
text (unicode on py2, str on py3) around 0MQ.
- u : Python unicode string (unicode on py2, str on py3)
- The unicode string to send.
- flags : int, optional
- Any valid send flag.
- encoding : str [default: ‘utf-8’]
- The encoding to be used
-
send_unicode(u, flags=0, copy=False, encoding='utf-8')
s.send_string(u, flags=0, copy=False, encoding=’utf-8’)
Send a Python unicode string as a message with an encoding.
0MQ communicates with raw bytes, so you must encode/decode
text (unicode on py2, str on py3) around 0MQ.
- u : Python unicode string (unicode on py2, str on py3)
- The unicode string to send.
- flags : int, optional
- Any valid send flag.
- encoding : str [default: ‘utf-8’]
- The encoding to be used
-
setsockopt(option, optval)
Set socket options.
See the 0MQ API documentation for details on specific options.
- option : int
The option to set. Available values will depend on your
version of libzmq. Examples include:
zmq.SUBSCRIBE, UNSUBSCRIBE, IDENTITY, HWM, LINGER, FD
- optval : int or bytes
- The value of the option to set.
-
setsockopt_string(option, optval, encoding='utf-8')
Set socket options with a unicode object it is simply a wrapper
for setsockopt to protect from encoding ambiguity.
See the 0MQ documentation for details on specific options.
- option : int
- The name of the option to set. Can be any of: SUBSCRIBE,
UNSUBSCRIBE, IDENTITY
- optval : unicode string (unicode on py2, str on py3)
- The value of the option to set.
- encoding : str
- The encoding to be used, default is utf8
-
setsockopt_unicode(option, optval, encoding='utf-8')
s.setsockopt_string(option, optval, encoding=’utf-8’)
Set socket options with a unicode object it is simply a wrapper
for setsockopt to protect from encoding ambiguity.
See the 0MQ documentation for details on specific options.
- option : int
- The name of the option to set. Can be any of: SUBSCRIBE,
UNSUBSCRIBE, IDENTITY
- optval : unicode string (unicode on py2, str on py3)
- The value of the option to set.
- encoding : str
- The encoding to be used, default is utf8
zmq – The pyzmq ØMQ python bindings
pyzmq Is a python binding to the C++ ØMQ library written in Cython . 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, DEALER, 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.