mahos.node.comm.Context#
- class mahos.node.comm.Context(context: 'Context' | zmq.Context | None = None, poll_timeout_ms: int | None = None, linger_ms: int | None = None)#
The communication context for mahos nodes.
- Parameters:
context – if passed, internal ZMQ context is shared.
poll_timeout_ms – polling timeout in milliseconds.
Current implementation is based on ZMQ.
Communication is divided into two classes: 1. Inbound (rep, sub, broker) 2. Outbound (req, pub)
When adding inbound communication, register a function
handler. Thehandleris called when an inbound message is received. Thepoll()method polls and handles all registered inbound messages. (So, users don’t have to care about it.)When adding outbound communication, a sender object (Requester or Publisher) is returned. Through that object, users can send messages (and receive replies for Requester) at their own timing.
Note that context itself is thread-safe, but derived objects (ZMQ sockets) are not. Let’s consider a use case: You start a sub-thread for inbound message polling (in a loop), and use main-thread for outbound message sending (and provide API to get inbound message, etc.). In this case, only sub-thread should add inbound messages and use poll() method; and only main-thread should add outbound messages and use sender objects. See mahos.node.client for example implementations.
- __init__(context: 'Context' | zmq.Context | None = None, poll_timeout_ms: int | None = None, linger_ms: int | None = None)#
Methods
__init__([context, poll_timeout_ms, linger_ms])add_broker(xpub_endpoint, xsub_endpoint[, ...])Add broker (xpub/xsub) handlers.
add_pub(endpoint, topic[, logger])Add and return a Publisher.
add_pub_handler(endpoint[, root_topic])Add PUBHandler for logging.
add_rep(endpoint[, handler, req_type])Add rep handler.
add_req(endpoint[, timeout_ms, rep_type, logger])Add and return a Requester.
add_sub(endpoint[, topic, handler, ...])Add sub handler.
close([close_zmq_ctx])Close this context.
poll()Poll inbound sockets and call corresponding handlers.
zmq_context()Get internal ZMQ context.