Communication between modules (pyobs.comm)
The Comm object is responsible for all communication between modules (see pyobs.modules
). The base
class for all implementations is Comm
.
The user usually only has contact with the Comm object when writing the configuration for an existing module or when developing a new module that needs to communicate with other modules.
In a configuration file, the Comm object is defined at top-level like this:
comm:
class: pyobs.comm.slixmpp.XmppComm
Except for a single parameter defined in Comm
’s constructor, all parameters are defined in
derived classes.
The most convenient way for getting access to other modules’ method is by using a Proxy
object,
which can easily be obtained by using the proxy()
method or the []
operator like this (if
the module named ‘camera’ implements the ICamera
interface):
camera = comm['camera']
camera.expose().wait()
Note that camera is now not of type ICamera
.
Each Module
that was configured with a Comm object (see modules
) has
an attribute comm
for easy access.
There is currently one one implementation of the Comm interface:
XmppComm
uses the XMPP protocol for communication.
See also
- Module
modules
Description for modules, to which Comm objects are usually assigned.
Comm
- class Comm(cache_proxies: bool = True)
Base class for all Comm modules in pyobs.
Creates a comm module.
- property clients: List[str]
Returns list of currently connected clients.
- Returns:
(list) List of currently connected clients.
- async clients_with_interface(interface: Type[pyobs.interfaces.Interface]) List[str] [source]
Returns list of currently connected clients that implement the given interface.
- Parameters:
interface – Interface to search for.
- Returns:
(list) List of currently connected clients that implement the given interface.
- async execute(client: str, method: str, signature: inspect.Signature, *args: Any) pyobs.utils.parallel.Future [source]
Execute a given method on a remote client.
- Parameters:
client (str) – ID of client.
method (str) – Method to call.
signature – Method signature.
*args – List of parameters for given method.
- Returns:
Passes through return from method call.
- async get_interfaces(client: str) List[Type[pyobs.interfaces.Interface]] [source]
Returns list of interfaces for given client.
- Parameters:
client – Name of client.
- Returns:
List of supported interfaces.
- Raises:
IndexError – If client cannot be found.
- log_message(entry: pyobs.events.LogEvent) None [source]
Send a log message to other clients.
- Parameters:
entry (LogEvent) – Log event to send.
- property name: Optional[str]
Name of this client.
- async proxy(name_or_object: Union[str, object], obj_type: Optional[Type[pyobs.comm.comm.ProxyType]] = None) Union[Any, pyobs.comm.comm.ProxyType] [source]
Returns object directly if it is of given type. Otherwise get proxy of client with given name and check type.
- If name_or_object is an object:
If it is of type (or derived), return object.
Otherwise raise exception.
- If name_name_or_object is string:
Create proxy from name and raise exception, if it doesn’t exist.
Check type and raise exception if wrong.
Return object.
- Parameters:
name_or_object – Name of object or object itself.
obj_type – Expected class of object.
- Returns:
Object or proxy to object.
- Raises:
ValueError – If proxy does not exist or wrong type.
- async register_event(event_class: Type[pyobs.events.Event], handler: Optional[Callable[[pyobs.events.Event, str], collections.abc.Coroutine[Any, Any, bool]]] = None) None [source]
Register an event type. If a handler is given, we also receive those events, otherwise we just send them.
- Parameters:
event_class – Class of event to register.
handler – Event handler method.
- async safe_proxy(name_or_object: Union[str, object], obj_type: Optional[Type[pyobs.comm.comm.ProxyType]] = None) Optional[Union[Any, pyobs.comm.comm.ProxyType]] [source]
Calls proxy() in a safe way and returns None instead of raising an exception.
- async send_event(event: pyobs.events.Event) None [source]
Send an event to other clients.
- Parameters:
event (Event) – Event to send
Proxy
- class Proxy(comm: Comm, client: str, interfaces: List[Type[Interface]])
A proxy for remote pyobs modules.
Creates a new proxy.
- Parameters:
comm – Comm object to use for connection.
client – Name of client to connect to.
interfaces – List of interfaces supported by client.
- async execute(method: str, *args: Any, **kwargs: Any) Any [source]
Execute a method on the remote client.
- Parameters:
method – Name of method to call.
*args – Parameters for method call.
**kwargs – Parameters for method call.
- Returns:
Result of method call.
- interface_method(method: str) Any [source]
Returns the method of the given name from the interface and not from the object itself.
- Parameters:
method – Name of method.
- Returns:
The interface method.
- property interfaces: List[Type[pyobs.interfaces.Interface]]
List of interfaces.
- property method_names: List[str]
List of method names.
- property name: str
Name of the client.