Socket Operations

socket_operations.py

Functions for creating and using sockets for inter-process communication (IPC). The code below shows a minimal example.

In one process:

import socket_operations as so
from options import Options
options = Options()
sock = so.create_sockets(
    options.router_address,
    "BOREALIS_USER"
)
so.send_string(sock, "OTHER_BOREALIS_USER", "Good choice")

In another process:

import socket_operations as so
from options import Options
options = Options()
sock = so.create_sockets(
    options.router_address,
    "OTHER_BOREALIS_USER"
)
msg = so.recv_string(sock, "BOREALIS_USER")
assert msg == "Good choice"
todo:

log.debug all functions

src.utils.socket_operations.create_sockets(router_addr, *identities)[source]

Creates a DEALER socket for each identity in the list argument. Each socket is then connected to the router.

Parameters:
  • router_addr (str) – Address of the router socket

  • identities (str) – Unique identities to give to sockets

Returns:

Newly created and connected sockets.

Return type:

Union[zmq.Socket, list[zmq.Socket]]

src.utils.socket_operations.recv_bytes(socket, sender_identity, log=None)[source]

Receives data from a socket and verifies it comes from the correct sender.

Parameters:
  • socket (zmq.Socket) – Socket to recv from

  • sender_identity (str) – Identity of the expected sender

  • log (Optional[Any]) – A logging object

Returns:

Received data

Return type:

Optional[bytes]

src.utils.socket_operations.recv_bytes_from_any_iden(socket)[source]

Receives data from a socket, returns just the data and strips off the identity

Parameters:

socket (zmq.Socket) – Socket to recv from

Returns:

Received data

Return type:

Optional[bytes]

src.utils.socket_operations.recv_pyobj(socket, sender_identity, log=None, expected_type=None)[source]

Receive a pickled Python object.

Can be used to check if the received object is of expected_type.

Parameters:
  • socket (zmq.Socket) – Socket to receive from

  • sender_identity (str) – The identity of the sender

  • log (Optional[Any]) – A logging object

  • expected_type (Optional[Any]) – The data type expected when receiving

Returns:

an object

Return type:

Any

src.utils.socket_operations.recv_string(socket, sender_identity, log=None)[source]

Receives data from a socket and verifies it comes from the correct sender.

Parameters:
  • socket (zmq.Socket) – Socket to recv from

  • sender_identity (str) – Identity of the expected sender

  • log (Optional[Any]) – A logging object

Returns:

Received data

Return type:

String or None

src.utils.socket_operations.send_bytes(socket, receiver_identity, bytes_object, log=None)[source]

Sends experiment to another identity.

Parameters:
  • socket (zmq.Socket) – Socket to send from

  • receiver_identity (str) – The identity to send to

  • bytes_object (bytes) – The bytes to send

  • log (Optional[Any]) – A logging object

src.utils.socket_operations.send_pyobj(socket, receiver_identity, message, log=None)[source]

Pickles the message and passes it to send bytes to be communicated over the router.

Parameters:
  • socket (zmq.Socket) – Socket to send from

  • receiver_identity (str) – The identity of the receiver

  • message (Any) – The object to send

  • log (Optional[Any]) – A logging object

src.utils.socket_operations.send_string(socket, receiver_identity, msg)[source]

Sends data to another identity.

Parameters:
  • socket (zmq.Socket) – Socket to send from

  • receiver_identity (str) – The identity to send to

  • msg (str) – The data message to send