narupa.core.narupa_client module

A module for setting up typical Narupa clients, containing a client that sets up a command service.

class narupa.core.narupa_client.NarupaClient(*, channel: grpc.Channel, make_channel_owner: bool = False)

Bases: narupa.core.grpc_client.GrpcClient

A base gRPC client for Narupa services. Automatically sets up a stub for the CommandServicer, enabling the running of arbitrary commands.

attempt_update_locks(lock_updates: Dict[str, Optional[float]]) → bool

Attempt to acquire and/or free a number of locks on the shared state. :param lock_updates: A dictionary of keys to either a duration in

seconds to attempt to acquire or renew a lock, or None to indicate the lock should be released if held.
Returns:True if the desired locks were acquired, and False otherwise.
attempt_update_state(change: narupa.utilities.change_buffers.DictionaryChange) → bool

Attempt to make a single atomic change to the shared state, blocking until a response is received. :param change: A single change to make to the shared state that will

either be made in full, or ignored if some of the keys are locked by another user.
Returns:True if the server accepted our change, and False otherwise.
available_commands

Returns a copy of the dictionary of commands available on the server, as determined by previously calling :fun:`update_available_commands`.

Returns:A dictionary of command information, keyed by the command names.
close()

Shutdown all threads and close the underlying channel if the client has been given that responsibility.

copy_state() → Dict[str, Union[None, str, int, float, bool, Iterable[Union[None, str, int, float, bool, Iterable[Any], Mapping[str, Any]]], Mapping[str, Union[None, str, int, float, bool, Iterable[Any], Mapping[str, Any]]]]]

Return a deep copy of the current state.

lock_state() → AbstractContextManager[Dict[str, Union[None, str, int, float, bool, Iterable[Union[None, str, int, float, bool, Iterable[Any], Mapping[str, Any]]], Mapping[str, Union[None, str, int, float, bool, Iterable[Any], Mapping[str, Any]]]]]]

Context manager that locks and returns the state. Any attempted state updates are delayed until the context is exited.

run_command(name: str, **arguments) → Dict[str, Union[None, str, int, float, bool, Iterable[Union[None, str, int, float, bool, Iterable[Any], Mapping[str, Any]]], Mapping[str, Union[None, str, int, float, bool, Iterable[Any], Mapping[str, Any]]]]]

Runs a command on the command server.

Parameters:
  • name – Name of command to run.
  • arguments – Arguments to provide to command.
Returns:

Dictionary of results, which may be empty.

subscribe_all_state_updates(interval=0.03333333333333333)

Subscribe, in the background, to any updates made to the shared state.

Parameters:interval – Minimum time (in seconds) between receiving new updates for any and all values.
update_available_commands() → Dict[str, narupa.command.command_info.CommandInfo]

Gets all the commands on the command server, and updates this client’s known commands. Blocks until the dictionary of available commands is received.

Returns:A dictionary of all the commands on the command server, keyed by name
class narupa.core.narupa_client.NarupaStubClient(*, channel: grpc.Channel, stub, make_channel_owner: bool = False)

Bases: narupa.core.narupa_client.NarupaClient

A base gRPC client for Narupa services. Automatically sets up a stub for the CommandServicer, and attaches the provided stub to the underlying gRPC channel.

Parameters:stub – gRPC stub to attach.