Async parameter tree
async_parameter_tree.py - classes representing an asychronous parameter tree and accessor.
This module defines a parameter tree and accessor for use in asynchronous API adapters, where concurrency over blocking operations (e.g. to read/write the value of a parameter from hardware) is required.
Tim Nicholls, STFC Detector Systems Software Group.
AsyncParameterAccessor
Bases: BaseParameterAccessor
Asynchronous container class representing accessor methods for a parameter.
This class extends the base parameter accessor class to support asynchronous set and get accessors for a parameter. Read-only and writeable parameters are supported, and the same metadata fields are implemented.
Note that the instantiation of objects of this class MUST be awaited to allow the getter function to evaluate and record the parameter type in the metadata, e.g.
accessor = await AsyncParameterAccessor(....)
Accessors instantiated during the intialisation of an AsyncParameterTree will automatically be collected and awaited by the tree itself.
Source code in src/odin_control/adapters/async_parameter_tree.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
__await__()
Make AsyncParameterAccessor objects awaitable.
This magic method makes the instantiation of AsyncParameterAccessor objects awaitable. This is required since instantiation must call the specified get() method, which is itself async, in order to resolve the type of the parameter and store that in the metadata. This cannot be done directly in the constructor.
Returns:
| Type | Description |
|---|---|
|
an awaitable future |
Source code in src/odin_control/adapters/async_parameter_tree.py
__init__(path, getter=None, setter=None, **kwargs)
Initialise the AsyncParameterAccessor instance.
This constructor initialises the AsyncParameterAccessor instance, storing the path of the parameter, its set/get accessors and setting metadata fields based on the the specified keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
path of the parameter within the tree |
required | |
getter
|
get method for the parameter, or a value if read-only constant |
None
|
|
setter
|
set method for the parameter |
None
|
|
kwargs
|
keyword argument list for metadata fields to be set; these must be from the allow list specified in BaseParameterAccessor.allowed_metadata |
{}
|
Source code in src/odin_control/adapters/async_parameter_tree.py
get(with_metadata=False)
async
Get the value of the parameter.
This async method returns the value of the parameter, or the value returned by the accessor getter, if one is defined (i.e. is callable). If the getter is itself async, the value is resolved by awaiting the returned coroutine. If the with_metadata argument is true, the value is returned in a dictionary including all metadata for the parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_metadata
|
include metadata in the response when set to True |
False
|
Returns:
| Type | Description |
|---|---|
|
value of the parameter |
Source code in src/odin_control/adapters/async_parameter_tree.py
resolve_coroutine(value)
async
staticmethod
Resolve a coroutine and return its value.
This static convenience method allows an accessor to resolve the output of its getter/setter functions to avalue if an async coroutine is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
value or coroutine to resolve |
required |
Returns:
| Type | Description |
|---|---|
|
resolved value |
Source code in src/odin_control/adapters/async_parameter_tree.py
set(value)
async
Set the value of the parameter.
This async method sets the value of the parameter by calling the set accessor if defined and callable. The result is awaited if a coroutine is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
value to set |
required |
Source code in src/odin_control/adapters/async_parameter_tree.py
AsyncParameterTree
Bases: BaseParameterTree
Class implementing an asynchronous tree of parameters and their accessors.
This async class implements an arbitrarily-structured, recursively-managed tree of parameters and the appropriate accessor methods that are used to read and write those parameters.
Note that the instantiation of an AsyncParameterTree MUST be awaited by calling code to allow the type and intial value of each parameter to be resolved, e.g.:
tree = await AsyncParameterTree(...)
Source code in src/odin_control/adapters/async_parameter_tree.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
__await__()
Make AsyncParameterTree objects awaitable.
This magic method makes the instantiation of AsyncParameterTree objects awaitable. This is required since the underlying accessor objects must also be awaited at initialisation to resolve their type and intial values. This is achieved by traversing the parameter tree and gathering all awaitable accessor instances and awaiting them.
Source code in src/odin_control/adapters/async_parameter_tree.py
__init__(tree, mutable=False)
Initialise the AsyncParameterTree object.
This constructor recursively initialises the AsyncParameterTree object based on the specified arguments. The tree initialisation syntax follows that of the BaseParameterTree implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
dict representing the parameter tree |
required | |
mutable
|
Flag, setting the tree |
False
|
Source code in src/odin_control/adapters/async_parameter_tree.py
get(path, with_metadata=False)
async
Get the values of parameters in a tree.
This async method returns the values at and below a specified path in the parameter tree. This is done by recursively populating the tree with the current values of parameters, returning the result as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
path in tree to get parameter values for |
required | |
with_metadata
|
include metadata in the response when set to True |
False
|
Returns:
| Type | Description |
|---|---|
|
dict of parameter tree at the specified path |
Source code in src/odin_control/adapters/async_parameter_tree.py
set(path, data)
async
Set the values of the parameters in a tree.
This async method sets the values of parameters in a tree, based on the data passed to it as a nested dictionary of parameter and value pairs. The updated parameters are merged into the existing tree recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
path to set parameters for in the tree |
required | |
data
|
nested dictionary representing values to update at the path |
required |