|
| 1 | +# micropython-aioschedule |
| 2 | + |
| 3 | +This project is very much a work in progress. Feel free to submit issues/PR's |
| 4 | + |
| 5 | +The concept is to have a simple micropython scheduler that is asynchronous, persistent (tasks are stored in flash and can be reloaded after a reboot/restart). |
| 6 | + |
| 7 | +Based on the python schedule library by dbader https://github.com/dbader/schedule |
| 8 | + |
| 9 | +Python job scheduling for humans. Run Python functions (or any other callable) periodically using a friendly syntax. |
| 10 | + |
| 11 | +- Asycnronous |
| 12 | +- A simple to use API for scheduling jobs, made for humans. |
| 13 | +- In-process scheduler for periodic jobs. No extra processes needed! |
| 14 | +- Persistence, tasks survive reboot/restart |
| 15 | +- Tested on MicroPython 1.19 |
| 16 | + |
| 17 | +Usage |
| 18 | +----- |
| 19 | + |
| 20 | +Requires datetime and functools from micropython-lib |
| 21 | + |
| 22 | +``` |
| 23 | +Copy schedule.py to your micropython lib directory |
| 24 | +``` |
| 25 | + |
| 26 | +```python |
| 27 | + |
| 28 | +import schedule |
| 29 | +import time |
| 30 | +import machine |
| 31 | +import uasyncio as asyncio |
| 32 | + |
| 33 | +async def job(): |
| 34 | + print("I'm working...") |
| 35 | + |
| 36 | +schedule.every(10).seconds.do(job) |
| 37 | +schedule.every(10).minutes.do(job) |
| 38 | +schedule.every().hour.do(job) |
| 39 | +schedule.every().day.at("10:30").do(job) |
| 40 | +schedule.every(5).to(10).minutes.do(job) |
| 41 | +schedule.every().monday.do(job) |
| 42 | +schedule.every().wednesday.at("13:15").do(job) |
| 43 | +schedule.every().minute.at(":17").do(job) |
| 44 | + |
| 45 | +async def job_with_argument(name): |
| 46 | + print(f"I am {name}") |
| 47 | + |
| 48 | +schedule.every(10).seconds.do(job_with_argument, name="Peter") |
| 49 | + |
| 50 | +async def main_loop(): |
| 51 | + # Run all outstanding tasks |
| 52 | + # Schedule will yield after all tasks have been run |
| 53 | + await schedule.run_pending() |
| 54 | + |
| 55 | + # Run any other user code here |
| 56 | + |
| 57 | + # Save schedule to flash |
| 58 | + schedule.save_flash() |
| 59 | + |
| 60 | + # Go to sleep until the next task is due |
| 61 | + idle_secs = schedule.idle_seconds() |
| 62 | + logger.debug(f'Going to sleep for { idle_secs } seconds') |
| 63 | + machine.deepsleep(int(idle_secs) * 1000) |
| 64 | + |
| 65 | +# Load schedule from flash |
| 66 | +schedule.load_flash() |
| 67 | + |
| 68 | +try: |
| 69 | + asyncio.run(main_loop()) |
| 70 | +except KeyboardInterrupt: |
| 71 | + print('Interrupted') |
| 72 | +except Exception as e: |
| 73 | + print("caught") |
| 74 | + print_exception(e) |
| 75 | +finally: |
| 76 | + asyncio.new_event_loop() |
| 77 | + |
| 78 | +``` |
| 79 | +```python |
| 80 | +# Save schedule to flash |
| 81 | +schedule.save_flash() |
| 82 | + |
| 83 | +# Load schedule from flash |
| 84 | +schedule.load_flash() |
| 85 | +``` |
| 86 | + |
| 87 | +Documentation |
| 88 | +------------- |
| 89 | + |
| 90 | +TODO |
| 91 | + |
| 92 | + |
| 93 | +Meta |
| 94 | +---- |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +Inspired by Daniel Bader - `@dbader_org <https://twitter.com/dbader_org>`_ - [email protected], |
| 99 | +`Adam Wiggins' <https://github.com/adamwiggins>`_ article `"Rethinking Cron" <https://adam.herokuapp.com/past/2010/4/13/rethinking_cron/>`_ |
| 100 | +and the `clockwork <https://github.com/Rykian/clockwork>`_ Ruby module. |
| 101 | + |
| 102 | +Distributed under the MIT license. See `LICENSE.txt <https://github.com/ThinkTransit/schedule-micropython/blob/master/LICENSE.txt>`_ for more information. |
| 103 | + |
| 104 | +https://github.com/ThinkTransit/schedule-micropython |
0 commit comments