1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later
"""Demo Tango Device Server using asyncio green mode"""
import asyncio
from tango import DevState, GreenMode
from tango.server import Device, command, attribute
class AsyncioDevice(Device):
green_mode = GreenMode.Asyncio
async def init_device(self):
await super().init_device()
self.set_state(DevState.ON)
@command
async def long_running_command(self):
self.set_state(DevState.OPEN)
await asyncio.sleep(2)
self.set_state(DevState.CLOSE)
@command
async def background_task_command(self):
loop = asyncio.get_event_loop()
future = loop.create_task(self.coroutine_target())
async def coroutine_target(self):
self.set_state(DevState.INSERT)
await asyncio.sleep(15)
self.set_state(DevState.EXTRACT)
@attribute
async def test_attribute(self):
await asyncio.sleep(2)
return 42
if __name__ == "__main__":
AsyncioDevice.run_server()
|