How to check if the code is being executed by a server process?

Hi,

I would like to check with PyTango if the code is being executed by a Tango DS:
I was trying this:


import tango

try:
tango.Util.instance()
except:
print("We are not in Tango DS")
else:
print("We are in Tango DS")


But it exits the process:

$> python3 foo.py
Tango is not initialised !!!
Exiting


Any hint would be appreciated.
Thanks!
Zibi
Edited 3 years ago
Hi Zibi

I guess you are asking for a non-fatal way to check? Your example is not in a TangoDS, so we expect an error.

It turns out the tango.Util.instance() method takes an optional (and undocumented) parameter in PyTango, and that parameter is whether or not to exit on error. cppTango code here. Set it to false, if you don't want to exit.


$ python
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tango
>>> tango.Util.instance(False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
PyTango.DevFailed: DevFailed[
DevError[
desc = Util singleton not created
origin = Util::instance
reason = API_UtilSingletonNotCreated
severity = ERR]
]
>>> tango.Util.instance(True)
Tango is not initialised !!!
Exiting


Regards,
Anton
Many thanks Anton!
This was what I was looking for.
Just in case it makes sense to (re)add it to the docs, I opened this PR: https://github.com/tango-controls/pytango/pull/408.
Let's discuss it there.
 
Register or login to create to post a reply.