如何使用asyncio使python decorator函数在n秒内重新调度函数?

时间:2016-08-23 01:12:10

标签: python decorator python-asyncio

这是我目前的代码:

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()

在这个例子中,我试图安排send_command函数在5秒内运行,之后每隔10秒运行一次。它似乎有效,但我对loop.call_at(loop.time() + seconds, decorated, *args)电话有点困惑。任何意见将不胜感激。

1 个答案:

答案 0 :(得分:1)

你的代码很好(除非你注意到你应该在装饰器内返回func的结果),我对它进行了一些改进。

使用合成示例更容易查看和测试:

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())

输出:

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]