# asyncio mode

Asyncio mode is similar to gevent but it uses explicit coroutines. You can compare gevent and asyncio examples.

     1import asyncio
     2from tango.asyncio import DeviceProxy
     3
     4async def asyncio_example():
     5    dev = await DeviceProxy("sys/tg_test/1")
     6    print(dev.get_green_mode())
     7
     8    print(await dev.state())
     9
    10loop = asyncio.get_event_loop()
    11loop.run_until_complete(asyncio_example())
    12loop.close()

Below you can find a TCP server example, which runs in an asynchronous mode and waits for a device’s attribute name from a TCP client, then asks the device for a value and replies to the TCP client.

     1"""A simple TCP server for Tango attributes.
     2
     3It runs on all interfaces on port 8888:
     4
     5   $ python tango_tcp_server.py
     6   Serving on 0.0.0.0 port 8888
     7
     8It can be accessed using netcat:
     9
    10   $ ncat localhost 8888
    11   >>> sys/tg_test/1/ampli
    12   0.0
    13   >>> sys/tg_test/1/state
    14   RUNNING
    15   >>> sys/tg_test/1/nope
    16   DevFailed[
    17   DevError[
    18        desc = Attribute nope is not supported by device sys/tg_test/1
    19      origin = AttributeProxy::real_constructor()
    20      reason = API_UnsupportedAttribute
    21    severity = ERR]
    22    ]
    23   >>> ...
    24"""
    25
    26import asyncio
    27from tango.asyncio import AttributeProxy
    28
    29
    30async def handle_echo(reader, writer):
    31    # Write the cursor
    32    writer.write(b'>>> ')
    33    # Loop over client request
    34    async for line in reader:
    35        request = line.decode().strip()
    36        # Get attribute value using asyncio green mode
    37        try:
    38            proxy = await AttributeProxy(request)
    39            attr_value = await proxy.read()
    40            reply = str(attr_value.value)
    41        # Catch exception if something goes wrong
    42        except Exception as exc:
    43            reply = str(exc)
    44        # Reply to client
    45        writer.write(reply.encode() + b'\n' + b'>>> ')
    46    # Close communication
    47    writer.close()
    48
    49
    50async def start_serving():
    51    server = await asyncio.start_server(handle_echo, '0.0.0.0', 8888)
    52    print('Serving on {} port {}'.format(*server.sockets[0].getsockname()))
    53    return server
    54
    55
    56async def stop_serving(server):
    57    server.close()
    58    await server.wait_closed()
    59
    60
    61def main():
    62    # Start the server
    63    loop = asyncio.get_event_loop()
    64    server = loop.run_until_complete(start_serving())
    65    # Serve requests until Ctrl+C is pressed
    66    try:
    67        loop.run_forever()
    68    except KeyboardInterrupt:
    69        pass
    70    # Close the server
    71    loop.run_until_complete(stop_serving(server))
    72    loop.close()
    73
    74
    75if __name__ == '__main__':
    76    main()