3

This is my current code:

import asyncio
import serial


def repeat(seconds):
    def wrap(func):
        def decorated(*args):
            loop = asyncio.get_event_loop()
            loop.call_at(loop.time() + seconds, decorated, *args)
            print('scheduled')
            func(*args)
            return func
        return decorated
    return wrap


@repeat(10)
def send_command(ser, text):
    try:
        if text == 'DATA':
            print("\nSending Data Request")
            ser.write(b'\n022022')
        elif text == 'STAY':
            print("\nInmediate Stay ARM")
            ser.write(b'\n0640010002044D')
        elif text == 'AWAY':
            print("\nInmediate Away ARM")
            ser.write(b'\n0640010003044E')
        elif text == 'DISARM':
            print("\nDISARM")
            ser.write(b'\n0940010001040700055B')
        elif text == 'CHIME':
            print("\nChime toggle")
            ser.write(b'\n0640010007014F')
    except serial.SerialException as e:
        raise e


if __name__ == '__main__':

    ser = serial.Serial('/dev/ttyUSB0', 9600, parity=serial.PARITY_ODD, write_timeout=0, timeout=0)

    loop = asyncio.get_event_loop()
    loop.call_at(loop.time() + 5, send_command, ser, 'DATA')
    loop.run_forever()

In this example I'm trying to schedule the send_command function to run in 5 seconds and every 10 seconds afterwards. It seems to work but I'm a bit confused with the loop.call_at(loop.time() + seconds, decorated, *args) call. Any comments will be appreciated.

  • not commenting on `loop.call_at`, your code seems not working at `return func`, which should be `func(*args)` – Lei Shi Aug 23 '16 at 01:57
  • And could you elaborate which part of `loop.call_at` confused you? according to the [doc](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.call_at), it just schedules the callback (`decorated`) to be called at the given time point (`loop.time() + seconds`) – Lei Shi Aug 23 '16 at 02:02
  • Sorry I did not include the actual function call func(*args). Already edited. I'm confused with the call of the inner function (decorated) to reschedule it into the future. – Angel Talavera Aug 23 '16 at 02:39

1 Answers1

1

Your code is fine (except as noted you should return func's result inside decorator), I improved it a bit.

It's easier to see and test with synthetic example:

import asyncio
import functools


def repeat(seconds):
    def wrap(func):
        @functools.wraps(func)  # see http://stackoverflow.com/q/308999/1113207
        def decorated(*args, **kwargs):
            # We should call func that decorated again to
            # force decorator's `call_at` code to execute again:
            loop = asyncio.get_event_loop()
            loop.call_at(
                loop.time() + seconds, 
                functools.partial(decorated, *args, **kwargs)  # see http://stackoverflow.com/q/3252228/1113207
            )
            # We should return result of func's excecution:
            return func(*args, **kwargs)
        return decorated
    return wrap


@repeat(2)
def send_command():
    print('send_command')


async def main():
    send_command()  # call once, rescheduling started

    for i in range(10):
        print(i)
        await asyncio.sleep(1)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Output:

send_command
0
1
send_command
2
3
send_command
4
5
send_command
6
7
send_command
8
9
send_command
[Finished in 10.3s]
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159