PyTango supports cooperative green Tango objects. Since version 8.1 two green modes have been added: Futures and Gevent.
The Futures uses the standard python module concurrent.futures. The Gevent mode uses the well known gevent library.
Currently, in version 8.1, only DeviceProxy has been modified to work in a green cooperative way. If the work is found to be useful, the same can be implemented in the future for AttributeProxy and even to Database.
You can set the PyTango green mode at a global level. Set the environment variable PYTANGO_GREEN_MODE to either futures or gevent (case incensitive). If this environment variable is not defined the PyTango global green mode defaults to Synchronous.
You can also change the active global green mode at any time in your program:
>>> from PyTango import DeviceProxy, GreenMode
>>> from PyTango import set_green_mode, get_green_mode
>>> get_green_mode()
PyTango.GreenMode.Synchronous
>>> dev = DeviceProxy("sys/tg_test/1")
>>> dev.get_green_mode()
PyTango.GreenMode.Synchronous
>>> set_green_mode(GreenMode.Futures)
>>> get_green_mode()
PyTango.GreenMode.Futures
>>> dev.get_green_mode()
PyTango.GreenMode.Futures
As you can see by the example, the global green mode will affect any previously created DeviceProxy using the default DeviceProxy constructor parameters.
You can specificy green mode on a DeviceProxy at creation time. You can also change the green mode at any time:
>>> from PyTango.futures import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> dev.get_green_mode()
PyTango.GreenMode.Futures
>>> dev.set_green_mode(GreenMode.Synchronous)
>>> dev.get_green_mode()
PyTango.GreenMode.Synchronous
Using concurrent.futures cooperative mode in PyTango is relatively easy:
>>> from PyTango.futures import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> dev.get_green_mode()
PyTango.GreenMode.Futures
>>> print(dev.state())
RUNNING
The PyTango.futures.DeviceProxy() API is exactly the same as the standard DeviceProxy. The difference is in the semantics of the methods that involve synchronous network calls (constructor included) which may block the execution for a relatively big amount of time. The list of methods that have been modified to accept futures semantics are, on the PyTango.futures.DeviceProxy():
So how does this work in fact? I see no difference from using the standard DeviceProxy. Well, this is, in fact, one of the goals: be able to use a futures cooperation without changing the API. Behind the scenes the methods mentioned before have been modified to be able to work cooperatively.
All of the above methods have been boosted with two extra keyword arguments wait and timeout which allow to fine tune the behaviour. The wait parameter is by default set to True meaning wait for the request to finish (the default semantics when not using green mode). If wait is set to True, the timeout determines the maximum time to wait for the method to execute. The default is None which means wait forever. If wait is set to False, the timeout is ignored.
If wait is set to True, the result is the same as executing the standard method on a DeviceProxy. If, wait is set to False, the result will be a concurrent.futures.Future. In this case, to get the actual value you will need to do something like:
>>> from PyTango.futures import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> result = dev.state(wait=False)
>>> result
<Future at 0x16cb310 state=pending>
>>> # this will be the blocking code
>>> state = result.result()
>>> print(state)
RUNNING
Here is another example using read_attribute():
>>> from PyTango.futures import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> result = dev.read_attribute('wave', wait=False)
>>> result
<Future at 0x16cbe50 state=pending>
>>> dev_attr = result.result()
>>> print(dev_attr)
DeviceAttribute[
data_format = PyTango.AttrDataFormat.SPECTRUM
dim_x = 256
dim_y = 0
has_failed = False
is_empty = False
name = 'wave'
nb_read = 256
nb_written = 0
quality = PyTango.AttrQuality.ATTR_VALID
r_dimension = AttributeDimension(dim_x = 256, dim_y = 0)
time = TimeVal(tv_nsec = 0, tv_sec = 1383923329, tv_usec = 451821)
type = PyTango.CmdArgType.DevDouble
value = array([ -9.61260664e-01, -9.65924853e-01, -9.70294813e-01,
-9.74369212e-01, -9.78146810e-01, -9.81626455e-01,
-9.84807087e-01, -9.87687739e-01, -9.90267531e-01,
...
5.15044507e-1])
w_dim_x = 0
w_dim_y = 0
w_dimension = AttributeDimension(dim_x = 0, dim_y = 0)
w_value = None]
Warning
Before using gevent mode please note that at the time of writing this documentation, PyTango.gevent requires the latest version 1.0 of gevent (which has been released the day before :-P). Also take into account that gevent 1.0 is not available on python 3. Please consider using the Futures mode instead.
Using gevent cooperative mode in PyTango is relatively easy:
>>> from PyTango.gevent import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> dev.get_green_mode()
PyTango.GreenMode.Gevent
>>> print(dev.state())
RUNNING
The PyTango.gevent.DeviceProxy() API is exactly the same as the standard DeviceProxy. The difference is in the semantics of the methods that involve synchronous network calls (constructor included) which may block the execution for a relatively big amount of time. The list of methods that have been modified to accept gevent semantics are, on the PyTango.gevent.DeviceProxy():
So how does this work in fact? I see no difference from using the standard DeviceProxy. Well, this is, in fact, one of the goals: be able to use a gevent cooperation without changing the API. Behind the scenes the methods mentioned before have been modified to be able to work cooperatively with other greenlets.
All of the above methods have been boosted with two extra keyword arguments wait and timeout which allow to fine tune the behaviour. The wait parameter is by default set to True meaning wait for the request to finish (the default semantics when not using green mode). If wait is set to True, the timeout determines the maximum time to wait for the method to execute. The default timeout is None which means wait forever. If wait is set to False, the timeout is ignored.
If wait is set to True, the result is the same as executing the standard method on a DeviceProxy. If, wait is set to False, the result will be a gevent.event.AsyncResult. In this case, to get the actual value you will need to do something like:
>>> from PyTango.gevent import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> result = dev.state(wait=False)
>>> result
<gevent.event.AsyncResult at 0x1a74050>
>>> # this will be the blocking code
>>> state = result.get()
>>> print(state)
RUNNING
Here is another example using read_attribute():
>>> from PyTango.gevent import DeviceProxy
>>> dev = DeviceProxy("sys/tg_test/1")
>>> result = dev.read_attribute('wave', wait=False)
>>> result
<gevent.event.AsyncResult at 0x1aff54e>
>>> dev_attr = result.get()
>>> print(dev_attr)
DeviceAttribute[
data_format = PyTango.AttrDataFormat.SPECTRUM
dim_x = 256
dim_y = 0
has_failed = False
is_empty = False
name = 'wave'
nb_read = 256
nb_written = 0
quality = PyTango.AttrQuality.ATTR_VALID
r_dimension = AttributeDimension(dim_x = 256, dim_y = 0)
time = TimeVal(tv_nsec = 0, tv_sec = 1383923292, tv_usec = 886720)
type = PyTango.CmdArgType.DevDouble
value = array([ -9.61260664e-01, -9.65924853e-01, -9.70294813e-01,
-9.74369212e-01, -9.78146810e-01, -9.81626455e-01,
-9.84807087e-01, -9.87687739e-01, -9.90267531e-01,
...
5.15044507e-1])
w_dim_x = 0
w_dim_y = 0
w_dimension = AttributeDimension(dim_x = 0, dim_y = 0)
w_value = None]
Note
due to the internal workings of gevent, setting the wait flag to True (default) doesn’t prevent other greenlets from running in parallel. This is, in fact, one of the major bonus of working with gevent when compared with concurrent.futures
Returns the current global default PyTango green mode.
Returns: | the current global default PyTango green mode |
---|---|
Return type: | GreenMode |
Sets the global default PyTango green mode.
Advice: Use only in your final application. Don’t use this in a python library in order not to interfere with the beavior of other libraries and/or application where your library is being.
Parameters: | green_mode (GreenMode) – the new global default PyTango green mode |
---|
get_device_proxy(self, dev_name, need_check_acc, green_mode=None, wait=True, timeout=None) -> DeviceProxy
Returns a new DeviceProxy. There is no difference between using this function and the direct DeviceProxy constructor if you use the default kwargs.
The added value of this function becomes evident when you choose a green_mode to be Futures or Gevent. The DeviceProxy constructor internally makes some network calls which makes it slow. By using one of the green modes as green_mode you are allowing other python code to be executed in a cooperative way.
Note
The timeout parameter has no relation with the tango device client side timeout (gettable by get_timeout_millis() and settable through set_timeout_millis())
Parameters: |
|
---|---|
Returns: |
|
Throws: |
|
New in PyTango 8.1.0
get_attribute_proxy(self, device_proxy, attr_name, green_mode=None, wait=True, timeout=True) -> AttributeProxy
Returns a new AttributeProxy. There is no difference between using this function and the direct AttributeProxy constructor if you use the default kwargs.
The added value of this function becomes evident when you choose a green_mode to be Futures or Gevent. The AttributeProxy constructor internally makes some network calls which makes it slow. By using one of the green modes as green_mode you are allowing other python code to be executed in a cooperative way.
Parameters: |
|
---|---|
Returns: |
|
Throws: |
|
New in PyTango 8.1.0
This class allows you to access the tango syncronization model API. It is designed as a singleton. To get a reference to the singleton object you must do:
import PyTango
apiutil = PyTango.ApiUtil.instance()
New in PyTango 7.1.3
Get the asynchronous callback sub-model.
Parameters: None Return: (cb_sub_model) the active asynchronous callback sub-model. New in PyTango 7.1.3
Fire callback methods for all (any device) asynchronous requests (command and attribute) with already arrived replied. Returns immediately if there is no replies already arrived or if there is no asynchronous requests.
Parameters: None Return: None Throws: None, all errors are reported using the err and errors fields of the parameter passed to the callback method. New in PyTango 7.1.3
get_asynch_replies (self) -> None
Fire callback methods for all (any device) asynchronous requests (command and attributes) with already arrived replied. Wait and block the caller for timeout milliseconds if they are some device asynchronous requests which are not yet arrived. Returns immediately if there is no asynchronous request. If timeout is set to 0, the call waits until all the asynchronous requests sent has received a reply.
Parameters:
timeout: (int) timeout (milliseconds) Return: None
Throws: AsynReplyNotArrived. All other errors are reported using the err and errors fields of the object passed to the callback methods.
New in PyTango 7.1.3
Returns the ApiUtil singleton instance.
Parameters: None Return: (ApiUtil) a reference to the ApiUtil singleton object. New in PyTango 7.1.3
Return number of asynchronous pending requests (any device). The input parameter is an enumeration with three values which are:
- POLLING: Return only polling model asynchronous request number
- CALL_BACK: Return only callback model asynchronous request number
- ALL_ASYNCH: Return all asynchronous request number
Parameters:
req: (asyn_req_type) asynchronous request type Return: (int) the number of pending requests for the given type
New in PyTango 7.1.3
Set the asynchronous callback sub-model between the pull and push sub-model. The cb_sub_model data type is an enumeration with two values which are:
- PUSH_CALLBACK: The push sub-model
- PULL_CALLBACK: The pull sub-model
Parameters:
model: (cb_sub_model) the callback sub-model Return: None
New in PyTango 7.1.3
See also Event configuration information
A structure containing available alarm information for an attribute with the folowing members:
- min_alarm : (str) low alarm level
- max_alarm : (str) high alarm level
- min_warning : (str) low warning level
- max_warning : (str) high warning level
- delta_t : (str) time delta
- delta_val : (str) value delta
- extensions : (StdStringVector) extensions (currently not used)
A structure containing x and y attribute data dimensions with the following members:
- dim_x : (int) x dimension
- dim_y : (int) y dimension
A structure (inheriting from DeviceAttributeConfig) containing available information for an attribute with the following members:
- disp_level : (DispLevel) display level (OPERATOR, EXPERT)
Inherited members are:
- name : (str) attribute name
- writable : (AttrWriteType) write type (R, W, RW, R with W)
- data_format : (AttrDataFormat) data format (SCALAR, SPECTRUM, IMAGE)
- data_type : (int) attribute type (float, string,..)
- max_dim_x : (int) first dimension of attribute (spectrum or image attributes)
- max_dim_y : (int) second dimension of attribute(image attribute)
- description : (int) attribute description
- label : (str) attribute label (Voltage, time, ...)
- unit : (str) attribute unit (V, ms, ...)
- standard_unit : (str) standard unit
- display_unit : (str) display unit
- format : (str) how to display the attribute value (ex: for floats could be ‘%6.2f’)
- min_value : (str) minimum allowed value
- max_value : (str) maximum allowed value
- min_alarm : (str) low alarm level
- max_alarm : (str) high alarm level
- writable_attr_name : (str) name of the writable attribute
- extensions : (StdStringVector) extensions (currently not used)
A structure (inheriting from AttributeInfo) containing available information for an attribute with the following members:
- alarms : object containing alarm information (see AttributeAlarmInfo).
- events : object containing event information (see AttributeEventInfo).
- sys_extensions : StdStringVector
Inherited members are:
- name : (str) attribute name
- writable : (AttrWriteType) write type (R, W, RW, R with W)
- data_format : (AttrDataFormat) data format (SCALAR, SPECTRUM, IMAGE)
- data_type : (int) attribute type (float, string,..)
- max_dim_x : (int) first dimension of attribute (spectrum or image attributes)
- max_dim_y : (int) second dimension of attribute(image attribute)
- description : (int) attribute description
- label : (str) attribute label (Voltage, time, ...)
- unit : (str) attribute unit (V, ms, ...)
- standard_unit : (str) standard unit
- display_unit : (str) display unit
- format : (str) how to display the attribute value (ex: for floats could be ‘%6.2f’)
- min_value : (str) minimum allowed value
- max_value : (str) maximum allowed value
- min_alarm : (str) low alarm level
- max_alarm : (str) high alarm level
- writable_attr_name : (str) name of the writable attribute
- extensions : (StdStringVector) extensions (currently not used)
- disp_level : (DispLevel) display level (OPERATOR, EXPERT)
see also PyTango.AttributeInfo
A base structure containing available information for an attribute with the following members:
- name : (str) attribute name
- writable : (AttrWriteType) write type (R, W, RW, R with W)
- data_format : (AttrDataFormat) data format (SCALAR, SPECTRUM, IMAGE)
- data_type : (int) attribute type (float, string,..)
- max_dim_x : (int) first dimension of attribute (spectrum or image attributes)
- max_dim_y : (int) second dimension of attribute(image attribute)
- description : (int) attribute description
- label : (str) attribute label (Voltage, time, ...)
- unit : (str) attribute unit (V, ms, ...)
- standard_unit : (str) standard unit
- display_unit : (str) display unit
- format : (str) how to display the attribute value (ex: for floats could be ‘%6.2f’)
- min_value : (str) minimum allowed value
- max_value : (str) maximum allowed value
- min_alarm : (str) low alarm level
- max_alarm : (str) high alarm level
- writable_attr_name : (str) name of the writable attribute
- extensions : (StdStringVector) extensions (currently not used)
A device command info with the following members:
- cmd_name : (str) command name
- cmd_tag : command as binary value (for TACO)
- in_type : (CmdArgType) input type
- out_type : (CmdArgType) output type
- in_type_desc : (str) description of input type
- out_type_desc : (str) description of output type
New in PyTango 7.0.0
A device command info (inheriting from DevCommandInfo) with the following members:
- disp_level : (DispLevel) command display level
Inherited members are (from DevCommandInfo):
- cmd_name : (str) command name
- cmd_tag : (str) command as binary value (for TACO)
- in_type : (CmdArgType) input type
- out_type : (CmdArgType) output type
- in_type_desc : (str) description of input type
- out_type_desc : (str) description of output type
A structure containing available information for a device with the” following members:
- dev_class : (str) device class
- server_id : (str) server ID
- server_host : (str) host name
- server_version : (str) server version
- doc_url : (str) document url
A structure with information about the locker with the folowing members:
- ll : (PyTango.LockerLanguage) the locker language
- li : (pid_t / UUID) the locker id
- locker_host : (str) the host
- locker_class : (str) the class
pid_t should be an int, UUID should be a tuple of four numbers.
New in PyTango 7.0.0
A structure containing PollDevice information with the folowing members:
- dev_name : (str) device name
- ind_list : (sequence<int>) index list
New in PyTango 7.0.0
This is the fundamental type for RECEIVING data from device attributes.
It contains several fields. The most important ones depend on the ExtractAs method used to get the value. Normally they are:
- value : Normal scalar value or numpy array of values.
- w_value : The write part of the attribute.
See other ExtractAs for different possibilities. There are some more fields, these really fixed:
- name : (str)
- data_format : (AttrDataFormat) Attribute format
- quality : (AttrQuality)
- time : (TimeVal)
- dim_x : (int) attribute dimension x
- dim_y : (int) attribute dimension y
- w_dim_x : (int) attribute written dimension x
- w_dim_y : (int) attribute written dimension y
- r_rimension : (tuple) Attribute read dimensions.
- w_dimension : (tuple) Attribute written dimensions.
- nb_read : (int) attribute read total length
- nb_written : (int) attribute written total length
Get the time at which the attribute was read by the server.
Note: It’s the same as reading the “time” attribute.
Parameters: None Return: (TimeVal) The attribute read timestamp.
Returns the error stack reported by the server when the attribute was read.
Parameters: None Return: (sequence<DevError>)
Sets the write value dim x.
Parameters:
val: (int) new write dim x Return: None
New in PyTango 8.0.0
Sets the write value dim y.
Parameters:
val: (int) new write dim y Return: None
New in PyTango 8.0.0
Device data is the type used internally by Tango to deal with command parameters and return values. You don’t usually need to deal with it, as command_inout will automatically convert the parameters from any other type and the result value to another type.
You can still use them, using command_inout_raw to get the result in a DeviceData.
You also may deal with it when reading command history.
This is the fundamental type for sending and receiving data from device commands. The values can be inserted and extracted using the insert() and extract() methods.
Get the actual value stored in the DeviceData.
Parameters: None Return: Whatever is stored there, or None.
This method returns the Tango data type of the data inside the DeviceData object.
Parameters: None Return: The content arg type.
Inserts a value in the DeviceData.
Parameters:
data_type: value: (any) The value to insert Return: Whatever is stored there, or None.
It can be used to test whether the DeviceData object has been initialized or not.
Parameters: None Return: True or False depending on whether the DeviceData object contains data or not.
Bases: PyTango._PyTango.DeviceAttribute
See DeviceAttribute.
Bases: PyTango._PyTango.DeviceData
See DeviceData.