swat.CAS.retrieve

CAS.retrieve(self, _name_, **kwargs)

Call the action and aggregate the results

Parameters
_name_string

Name of the action

**kwargsany, optional

Arbitrary keyword arguments

Returns
CASResults object

See also

invoke()

Calls action, but does not retrieve results

Examples

The code below demonstrates how you invoke an action on the server and retrieve the results.

>>> out = s.retrieve('help')
>>> print(out.keys())
['builtins', 'casidx', 'casmeta', 'espact', 'tkacon', 'table', 'tkcsessn',
 'tkcstate']
>>> print(out['builtins'])
              name                                        description
0          addnode                           Add a node to the server
1             help                        Lists the available actions
2        listnodes                              List the server nodes
.
.
.

Status and performance information is also available on the returned object. Here is an example of an action call to an action that doesn’t exist.

>>> out = s.retrieve('foo')
>>> print(out.status)
'The specified action was not found.'
>>> print(out.severity)
2
>>> print(out.messages)
["ERROR: Action 'foo' was not found.",
 'ERROR: The CAS server stopped processing this action because of errors.']

Here is an example that demonstrates the performance metrics that are available.

>>> out = s.retrieve('help')
>>> print(out.performance)
<swat.CASPerformance object at 0x33b1c50>

Performance values are loaded lazily, but you can get a dictionary of all of them using the to_dict method.

>>> print(out.performance.to_dict())
{'system_cores': 1152L, 'memory_quota': 303759360L, 'cpu_user_time': 0.014995,
 'elapsed_time': 0.004200000000000001, 'system_nodes': 48L,
 'memory_system': 432093312L, 'cpu_system_time': 0.018999, 'memory': 150688L,
 'memory_os': 294322176L, 'system_total_memory': 4868538236928L}

Rather than having the retrieve method compile all of the results into one object, you can control how the responses and results from the server are handled in your own functions using the responsefunc or resultfunc keyword arguments.

The responsefunc argument allows you to specify a function that is called for each response from the server after the action is called. The resultfunc is called for each result in a response. These functions can not be used at the same time though. In the case where both are specified, only the resultfunc will be used. Below is an example of using a responsefunc function. This function closely mimics what the retrieve method does by default.

>>> def myfunc(response, connection, userdata):
...     if userdata is None:
...         userdata = {}
...     for key, value in response:
...         userdata[key] = value
...     return userdata
>>> out = s.retrieve('help', responsefunc=myfunc)
>>> print(out['builtins'])
              name                                        description
0          addnode                           Add a node to the server
1             help                        Lists the available actions
2        listnodes                              List the server nodes
.
.
.

The same result can be gotten using the resultfunc option as well.

>>> def myfunc(key, value, response, connection, userdata):
...    if userdata is None:
...       userdata = {}
...    userdata[key] = value
...    return userdata
>>> out = s.retrieve('help', resultfunc=myfunc)
>>> print(out['builtins'])
              name                                        description
0          addnode                           Add a node to the server
1             help                        Lists the available actions
2        listnodes                              List the server nodes
.
.
.