bag.interface.zmqwrapper

This module defines various wrapper around ZMQ sockets.

Module Contents

Classes

ZMQDealer

A class that interacts with a ZMQ dealer socket.

ZMQRouter

A class that interacts with a ZMQ router socket.

class bag.interface.zmqwrapper.ZMQDealer(port, pipeline=100, host='localhost', log_file=None)[source]

Bases: object

A class that interacts with a ZMQ dealer socket.

a dealer socket is an asynchronous socket that can issue multiple requests without needing to wait for an reply. This class encapsulates the ZMQ socket details and provide more convenient API to use.

Parameters:
  • port (int) – the port to connect to.

  • pipeline (int) – number of messages allowed in a pipeline. Only affects file transfer performance.

  • host (str) – the host to connect to.

  • log_file (str or None) – the log file. None to disable logging.

log_msg(msg)[source]

Log the given message

log_obj(msg, obj)[source]

Log the given object

close()[source]

Close the underlying socket.

send_obj(obj)[source]

Sends a python object using pickle serialization and zlib compression.

Parameters:

obj (any) – the object to send.

recv_obj(timeout=None, enable_cancel=False)[source]

Receive a python object, serialized with pickle and compressed with zlib.

Parameters:
  • timeout (int or None) – the timeout to wait in miliseconds. If None, wait indefinitely.

  • enable_cancel (bool) – If True, allows the user to press Ctrl-C to abort. For this to work, the other end must know how to process the stop request dictionary.

Returns:

obj – the received object. None if timeout reached.

Return type:

any

recv_msg()[source]

Receive a string message.

Returns:

msg – the received object.

Return type:

str

class bag.interface.zmqwrapper.ZMQRouter(port=None, min_port=5000, max_port=9999, pipeline=100, log_file=None)[source]

Bases: object

A class that interacts with a ZMQ router socket.

a router socket is an asynchronous socket that can receive multiple requests without needing to issue an reply. This class encapsulates the ZMQ socket details and provide more convenient API to use.

Parameters:
  • port (int or None) – the port to connect to. If None, then a random port between min_port and max_port will be chosen.

  • min_port (int) – the minimum random port number (inclusive).

  • max_port (int) – the maximum random port number (exclusive).

  • pipeline (int) – number of messages allowed in a pipeline. Only affects file transfer performance.

  • log_file (str or None) – the log file. None to disable logging.

get_port()[source]

Returns the port number.

is_closed()[source]

Returns True if this router is closed.

close()[source]

Close the underlying socket.

log_msg(msg)[source]

Log the given message

log_obj(msg, obj)[source]

Log the given object

send_msg(msg, addr=None)[source]

Sends a string message

Parameters:
  • msg (str) – the message to send.

  • addr (str or None) – the address to send the object to. If None, send to last sender.

send_obj(obj, addr=None)[source]

Sends a python object using pickle serialization and zlib compression.

Parameters:
  • obj (any) – the object to send.

  • addr (str or None) – the address to send the object to. If None, send to last sender.

poll_for_read(timeout)[source]

Poll this socket for given timeout for read event.

Parameters:

timeout (int) – timeout in miliseconds.

Returns:

status – nonzero value means that this socket is ready for read.

Return type:

int

recv_obj()[source]

Receive a python object, serialized with pickle and compressed with zlib.

Returns:

obj – the received object.

Return type:

any

get_last_sender_addr()[source]

Returns the address of the sender of last received message.

Returns:

addr – the last sender address

Return type:

str