Message

Classes:

Message([msg, expand_arrays, blosc])

A formatted message that takes value, sends it to id, who should call the listen method indicated by the key.

class Message(msg=None, expand_arrays=False, blosc: bool = True, **kwargs)[source]

Bases: object

A formatted message that takes value, sends it to id, who should call the listen method indicated by the key.

Additional message behavior can be indicated by passing flags

Numpy arrays given in the value field are automatically serialized and deserialized when sending and receiving using bas64 encoding and blosc compression.

id, to, sender, and key are required attributes, but any other key-value pair passed on init is added to the message’s attributes and included in the message. All arguments not indicated in the signature are passed in as kwargs and stored as attributes.

Can be indexed and set like a dictionary (message[‘key’], etc.)

Variables
  • id (str) – ID that uniquely identifies a message. format {sender.id}_{number}

  • to (str) – ID of socket this message is addressed to

  • sender (str) – ID of socket where this message originates

  • key (str) – Type of message, used to select a listen method to process it

  • value – Body of message, can be any type but must be JSON serializable.

  • timestamp (str) – Timestamp of message creation

  • ttl (int) – Time-To-Live, each message is sent this many times at max, each send decrements ttl.

  • flags (dict) –

    Flags determine additional message behavior. If a flag has no value associated with it, add it as a key with None as the value (eg. self.flags[‘MINPRINT’] = None), the value doesn’t matter.

    • MINPRINT - don’t print the value in logs (eg. when a large array is being sent)

    • NOREPEAT - sender will not seek, and recipients will not attempt to send message receipt confirmations

    • NOLOG - don’t log this message! for streaming, or other instances where the constant printing of the logger is performance prohibitive

Parameters
  • msg (str) – A serialized message made with serialize(). Optional – can be passed rather than the message attributes themselves if, for example, we’re receiving and reconstituting this message.

  • expand_arrays (bool) – If given a serialized message, if True, expand and deserialize the arrays. Otherwise leave serialized. For speed of message forwarding – don’t deserialize if we’re just forwarding this message.

  • blosc (bool) – If True (default), When serializing arrays, also compress with blosc. Stored as a flag

  • *args

  • **kwargs

Methods:

__getitem__(key)

Parameters

key

__setitem__(key, value)

Parameters
  • key

expand()

Don't decompress numpy arrays by default for faster IO, explicitly expand them when needed

__delitem__(key)

Parameters

key

__contains__(key)

Parameters

key

get_timestamp()

Get a Python timestamp

validate()

Checks if id, to, sender, and key are all defined.

serialize()

Serializes all attributes in __dict__ using json.

__getitem__(key)[source]
Parameters

key

__setitem__(key, value)[source]
Parameters
  • key

  • value

expand()[source]

Don’t decompress numpy arrays by default for faster IO, explicitly expand them when needed

Returns

__delitem__(key)[source]
Parameters

key

__contains__(key)[source]
Parameters

key

get_timestamp()[source]

Get a Python timestamp

Returns

Isoformatted timestamp from datetime

Return type

str

validate()[source]

Checks if id, to, sender, and key are all defined.

Returns

Does message have all required attributes set?

Return type

bool (True)

serialize()[source]

Serializes all attributes in __dict__ using json.

Returns

JSON serialized message.

Return type

str