proxystore.connectors.zmq¶
ZeroMQ-based distributed in-memory connector implementation.
Each host runs a ZeroMQServer
which stores objects in memory. The first
ZeroMQConnector created on a
host spawns the server if one is not already running. Objects are put in
the server on the same host, and keys contain the address of that server
so connectors on other hosts can get objects directly from it.
Warning
The servers do not authenticate clients or encrypt data, so anyone who can reach a server's port can read, write, and evict objects. Only use this connector within trusted networks which are not publicly accessible, such as the interconnect of an HPC cluster.
Note
Only the ZeroMQConnector
and its key and exception types are public. The server functions and
classes (e.g., ZeroMQServer
and spawn_server()) are
internal implementation details which may change between releases
without notice (see
Versioning and Compatibility).
Messages are multipart ZeroMQ messages. A request consists of a request ID
frame, a JSON header frame (e.g., {"op": "get", "obj_id": "..."}),
and optionally a data frame. The reply echoes the request ID and contains a
JSON header frame with the status and optionally a data frame. Data frames
are sent and received without copies.
ZeroMQKey
¶
Bases: NamedTuple
Key to objects stored in a ZeroMQ server.
Attributes:
-
obj_id(str) –Unique object ID.
-
peer_host(str) –Address of the server where the object is stored.
-
peer_port(int) –Port of the server where the object is stored.
ZeroMQConnector
¶
ZeroMQConnector(
port: int,
address: str | None = None,
interface: str | None = None,
timeout: float = 5,
request_timeout: float | None = 60,
clear: bool = False,
)
ZeroMQ-based distributed in-memory connector.
Warning
The servers do not authenticate clients or encrypt data, so anyone who can reach a server's port can read, write, and evict objects. Only use this connector within trusted networks which are not publicly accessible, such as the interconnect of an HPC cluster.
Warning
Objects are stored in the memory of the server process, and there is no limit to the memory used. Objects are lost when the server exits.
Note
The first connector created on a host spawns a
ZeroMQServer in a
new process which other connectors on the host will use. Closing the
connector does not stop the server by default (see clear), but the
server is stopped when the process which spawned it exits.
Example
Parameters:
-
port(int) –Port of the server on this host.
-
address(str | None, default:None) –Address of this host that other hosts can connect to. Takes precedence over
interfaceif both are provided. -
interface(str | None, default:None) –Network interface to get the address of this host from (e.g.,
'eth0'). Only supported on Linux. -
timeout(float, default:5) –Timeout in seconds to connect to or spawn the server on this host.
-
request_timeout(float | None, default:60) –Timeout in seconds to wait for a response from a server before raising a
ServerTimeoutError. When operating on multiple objects, the timeout applies to each response.Nonewaits forever. -
clear(bool, default:False) –Stop the server on this host, if it was spawned by this connector, when
close()is called. This will lose all objects stored in the server.
Raises:
-
ServerTimeoutError–If a server on this host could not be connected to or spawned within
timeoutseconds.
Source code in proxystore/connectors/zmq.py
close
¶
Close the connector.
Parameters:
-
clear(bool | None, default:None) –Stop the server on this host if it was spawned by this connector. This will lose all objects stored in the server. Overrides the default value of
clearprovided when the connector was instantiated.
Returns:
-
bool–If the server was stopped, removing its objects.
Source code in proxystore/connectors/zmq.py
config
¶
Get the connector configuration.
The configuration contains all the information needed to reconstruct the connector object.
Source code in proxystore/connectors/zmq.py
from_config
classmethod
¶
from_config(config: dict[str, Any]) -> ZeroMQConnector
Create a new connector instance from a configuration.
Parameters:
Source code in proxystore/connectors/zmq.py
evict
¶
evict(key: ZeroMQKey) -> None
Evict the object associated with the key.
Parameters:
-
key(ZeroMQKey) –Key associated with object to evict.
exists
¶
get
¶
get_batch
¶
Get a batch of serialized objects associated with the keys.
Parameters:
Returns:
-
list[BytesLike | None]–List with same order as
keyswith the serialized objects orNoneif the corresponding key does not have an associated object.
Source code in proxystore/connectors/zmq.py
new_key
¶
Create a new key.
Parameters:
-
obj(BytesLike | None, default:None) –Optional object which the key will be associated with. Ignored in this implementation.
Returns:
Source code in proxystore/connectors/zmq.py
put
¶
put_batch
¶
Put a batch of serialized objects in the store.
Parameters:
Returns:
-
list[ZeroMQKey]–List of keys with the same order as
objswhich can be used to retrieve the objects.
Source code in proxystore/connectors/zmq.py
set
¶
Set the object associated with a key.
Note
The Connector
provides write-once, read-many semantics. Thus,
set()
should only be called once per key, otherwise unexpected behavior
can occur.
Parameters:
-
key(ZeroMQKey) –Key that the object will be associated with.
-
obj(BytesLike) –Object to associate with the key.
Source code in proxystore/connectors/zmq.py
ZeroMQServer
¶
In-memory storage and request handling for a server.
Use run_server() to serve
requests from clients.
Source code in proxystore/connectors/zmq.py
handle
¶
Process a request.
Parameters:
-
frames(Sequence[Frame]) –Frames of the request, excluding the identity frame added by the router socket.
Returns:
Source code in proxystore/connectors/zmq.py
run_server
¶
run_server(
address: str,
port: int,
*,
stop: Event | None = None,
poll_interval: float = 0.1,
) -> None
Serve requests from clients until stopped.
Parameters:
-
address(str) –Address to bind to.
-
port(int) –Port to bind to.
-
stop(Event | None, default:None) –Event which stops the server when set. If
None, the server runs forever. -
poll_interval(float, default:0.1) –Max time in seconds between checking
stop.
Raises:
-
ZMQError–If the server fails to bind to the address and port.
Source code in proxystore/connectors/zmq.py
start_server
¶
Run a server until SIGINT or SIGTERM is received.
This is the entry point of the process started by
spawn_server().
Parameters:
Source code in proxystore/connectors/zmq.py
spawn_server
¶
Spawn a server in a new process.
If another process spawns a server on the same address and port at the
same time, the server spawned by this call will fail to bind and exit,
and None is returned because the other server can be used instead.
Parameters:
-
address(str) –Address the server will bind to.
-
port(int) –Port the server will bind to.
-
timeout(float, default:5) –Max time in seconds to wait for the server to start.
Returns:
-
Popen[bytes] | None–The process running the server or
Noneif the server was spawned by a different process.
Raises:
-
ServerTimeoutError–If the server does not start within
timeoutseconds.
Source code in proxystore/connectors/zmq.py
wait_for_server
¶
Wait until a server responds.
Parameters:
-
address(str) –Address of the server.
-
port(int) –Port of the server.
-
timeout(float, default:5) –Max time in seconds to wait for the server to respond.
Returns:
-
int–The process ID of the server.
Raises:
-
ServerTimeoutError–If the server does not respond within
timeoutseconds.
Source code in proxystore/connectors/zmq.py
get_interface_address
¶
Get the IPv4 address of a network interface.
Warning
This function is only supported on Linux.
Parameters:
-
interface(str) –Name of the network interface (e.g.,
'eth0').
Returns:
-
str–The IPv4 address of the interface.
Raises:
-
NotImplementedError–If not on Linux.
-
OSError–If the interface does not exist or has no IPv4 address.