narupa.core.narupa_server module

class narupa.core.narupa_server.NarupaServer(*, address: str, port: int, max_workers=128)

Bases: narupa.core.grpc_server.GrpcServer

A base for Narupa gRPC servers. Sets up a gRPC server, and automatically attaches a CommandService and StateService enabling the running of arbitrary commands and synchronisation of state.

close()

Stops the server.

Inheritors of this class should override this method with routines to stop services that are running.

commands

Gets the commands available on this server.

Returns:The commands, consisting of their names, callback and default parameters.
copy_state() → Dict[str, object]

Return a deep copy of the current state.

lock_state() → AbstractContextManager[Dict[str, object]]

Context manager for reading the current state while delaying any changes to it.

register_command(name: str, callback: Callable[[Dict[KT, VT]], Optional[Dict[KT, VT]]], default_arguments: Optional[Dict[KT, VT]] = None)

Registers a command with the CommandService running on this server.

Parameters:
  • name – Name of the command to register
  • callback – Method to be called whenever the given command name is run by a client.
  • default_arguments – A description of the arguments of the callback and their default values.
Raises:

ValueError – Raised when a command with the same name already exists.

setup_services()

Sets up the services, including the CommandService.

unregister_command(name)

Deletes a command from this service.

Parameters:name – Name of the command to delete
update_locks(access_token: object = None, acquire: Optional[Dict[str, float]] = None, release: Optional[Set[str]] = None)

Attempts to acquire and release locks on keys in the shared key/value store. If any of the locks cannot be acquired, none of them will be.

update_state(access_token: object, change: narupa.utilities.change_buffers.DictionaryChange)

Attempts an atomic update of the shared key/value store. If any key cannot be updates, no change will be made.