Skip to content

Instantly share code, notes, and snippets.

@tiagocoutinho
Last active April 2, 2020 15:59
Show Gist options
  • Save tiagocoutinho/521dffdfb6bf1935e640c6aa3333414d to your computer and use it in GitHub Desktop.
Save tiagocoutinho/521dffdfb6bf1935e640c6aa3333414d to your computer and use it in GitHub Desktop.
Tango Server restart "dynamic" attributes
import os
import sys
import time
import threading
from tango.server import Device, attribute, command
attrs = [
dict(name='voltage', dtype=float, label='Voltage', fget=lambda dev: 1.2345),
dict(name='power_level', dtype=str, label='Power level', fget=lambda d: 'high')
]
def restart():
# give some time for device to reply to restart command execution
time.sleep(0.01)
pid = os.getpid()
with open('/proc/{}/cmdline'.format(pid), 'rb') as fobj:
cmdline = fobj.read()
cmdline = cmdline.strip().split(b'\x00')
print('Restarting {}'.format(cmdline))
os.execl(sys.executable, *cmdline)
class Mixin:
"""Just to store attributes before the Device.__new__ gets called"""
pass
for attr in attrs:
setattr(Mixin, attr['name'], attribute(**attr))
class DevTest(Mixin, Device):
@command
def restart(self):
task = threading.Thread(target=restart)
task.start()
PowerSupply.run_server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment