https://blog.51cto.com/huangyg/2367088

    1. import datetime
    2. import schedule
    3. import time
    4. def func():
    5. now = datetime.datetime.now()
    6. ts = now.strftime('%Y-%m-%d %H:%M:%S')
    7. print('do func time :',ts)
    8. def func2():
    9. now = datetime.datetime.now()
    10. ts = now.strftime('%Y-%m-%d %H:%M:%S')
    11. print('do func2 time:',ts)
    12. def tasklist():
    13. #清空任务
    14. schedule.clear()
    15. #创建一个按秒间隔执行任务
    16. schedule.every(1).seconds.do(func)
    17. #创建一个按2秒间隔执行任务
    18. schedule.every(2).seconds.do(func2)
    19. #执行10S
    20. for i in range(10):
    21. schedule.run_pending()
    22. time.sleep(1)
    23. tasklist()