eventlet.green.zmq
– ØMQ support¶
pyzmq
[1] is a python binding to the C++ ØMQ [2] library written in Cython [3].
eventlet.green.zmq
is greenthread aware version of pyzmq.
The zmq
module wraps the Socket
and Context
found in pyzmq
to be non blocking.
-
class
eventlet.green.zmq.
Context
(io_threads=1, **kwargs)¶ Bases:
zmq.sugar.context.Context
Subclass of
zmq.Context
-
class
eventlet.green.zmq.
Socket
(context, socket_type)¶ Bases:
zmq.sugar.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 receiving is deferred to the hub (usingeventlet.hubs.trampoline()
) if azmq.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 Frame 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 : bytes, Frame
- The received message frame. If copy is False, then it will be a Frame, otherwise it will be bytes.
- ZMQError
- for any of the reasons zmq_msg_recv 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, Frame
- 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 Frame is passed.
- ZMQError
- If the send does not succeed for any reason.
-
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
If the port range is unspecified, the system will choose the port.
- 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.
-
disable_monitor
()¶ Shutdown the PAIR socket (created using get_monitor_socket) that is serving socket events.
New in version 14.4.
-
disconnect
(addr)¶ Disconnect from a remote 0MQ socket (undoes a call to connect).
New in version libzmq-3.2.
New in version 13.0.
- 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.
-
get
(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.
-
get_hwm
()¶ get the High Water Mark
On libzmq ≥ 3, this gets SNDHWM if available, otherwise RCVHWM
-
get_monitor_socket
(events=None, addr=None)¶ Return a connected PAIR socket ready to receive the event notifications.
New in version libzmq-4.0.
New in version 14.0.
- events : bitfield (int) [default: ZMQ_EVENTS_ALL]
- The bitmask defining which events are wanted.
- addr : string [default: None]
- The optional endpoint for the monitoring sockets.
- socket : (PAIR)
- The socket is already connected and ready to receive messages.
-
get_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.
- optval : unicode string (unicode on py2, str on py3)
- The value of the option as a unicode string.
-
getsockopt
(option)¶ s.get(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.
- optval : unicode string (unicode on py2, str on py3)
- The value of the option as a unicode string.
-
getsockopt_unicode
(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.
- optval : unicode string (unicode on py2, str on py3)
- The value of the option as a unicode string.
-
hwm
¶ get the High Water Mark
On libzmq ≥ 3, this gets SNDHWM if available, otherwise RCVHWM
-
monitor
(addr, flags)¶ Start publishing socket events on inproc. See libzmq docs for zmq_monitor for details.
While this function is available from libzmq 3.2, pyzmq cannot parse monitor messages from libzmq prior to 4.0.
- addr : str
- The inproc url used for monitoring. Passing None as the addr will cause an existing socket monitor to be deregistered.
- events : int [default: zmq.EVENT_ALL]
- The zmq event bitmask for which events will be sent to the monitor.
-
poll
(timeout=None, flags=1)¶ 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 specified 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) 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 Frame 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 : bytes, Frame
- The received message frame. If copy is False, then it will be a Frame, otherwise it will be bytes.
- ZMQError
- for any of the reasons zmq_msg_recv might fail.
-
recv_json
(flags=0, **kwargs)¶ receive a Python object as a message using json to serialize
Keyword arguments are passed on to json.loads
- 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')¶ 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
(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, Frame
- 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 Frame is passed.
- ZMQError
- If the send does not succeed for any reason.
-
send_json
(obj, flags=0, **kwargs)¶ send a Python object as a message using json to serialize
Keyword arguments are passed on to json.dumps
- 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
The zmq.SNDMORE flag is added to all msg parts before the last.
- 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=2)¶ 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. The default is pickle.DEFAULT_PROTOCOL where defined, and pickle.HIGHEST_PROTOCOL elsewhere.
-
send_string
(u, flags=0, copy=True, 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=True, 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
-
set
(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.
Warning
All options other than zmq.SUBSCRIBE, zmq.UNSUBSCRIBE and zmq.LINGER only take effect for subsequent socket bind/connects.
-
set_hwm
(value)¶ set the High Water Mark
On libzmq ≥ 3, this sets both SNDHWM and RCVHWM
Warning
New values only take effect for subsequent socket bind/connects.
-
set_string
(option, optval, encoding='utf-8')¶ set socket options with a unicode object
This 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
()¶ s.set(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.
Warning
All options other than zmq.SUBSCRIBE, zmq.UNSUBSCRIBE and zmq.LINGER only take effect for subsequent socket bind/connects.
-
setsockopt_string
(option, optval, encoding='utf-8')¶ set socket options with a unicode object
This 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')¶ set socket options with a unicode object
This 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
-
classmethod
shadow
(address)¶ Shadow an existing libzmq socket
address is the integer address of the libzmq socket or an FFI pointer to it.
New in version 14.1.
-
unbind
(addr)¶ Unbind from an address (undoes a call to bind).
New in version libzmq-3.2.
New in version 13.0.
- 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.
-
underlying
¶ The address of the underlying libzmq socket
[1] | http://github.com/zeromq/pyzmq |
[2] | http://www.zeromq.com |
[3] | http://www.cython.org |