{"id":13802619,"url":"https://github.com/fizista/micropython-mcron","last_synced_at":"2025-10-25T19:30:50.178Z","repository":{"id":46211455,"uuid":"262061784","full_name":"fizista/micropython-mcron","owner":"fizista","description":null,"archived":false,"fork":false,"pushed_at":"2021-11-06T13:51:13.000Z","size":36,"stargazers_count":35,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-30T06:32:54.151Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fizista.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-07T13:44:14.000Z","updated_at":"2024-04-26T04:45:36.000Z","dependencies_parsed_at":"2022-09-26T18:30:27.383Z","dependency_job_id":null,"html_url":"https://github.com/fizista/micropython-mcron","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fizista%2Fmicropython-mcron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fizista%2Fmicropython-mcron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fizista%2Fmicropython-mcron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fizista%2Fmicropython-mcron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fizista","download_url":"https://codeload.github.com/fizista/micropython-mcron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219865145,"owners_count":16555931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T00:01:48.713Z","updated_at":"2025-10-25T19:30:49.853Z","avatar_url":"https://github.com/fizista.png","language":"Python","readme":".. role:: bash(code)\n   :language: bash\n\n.. role:: python(code)\n   :language: python\n\n***************************\nMicroCRON for MicroPython\n***************************\n\nMicroCRON is a time-based task scheduling program.\n\nThe library was designed after the experience with its larger predecessor SimpleCRON_. The current library focuses\non minimalism of code while keeping maximum functionality. It takes 3 times less memory than SimpleCRON_.\nIt needs about 3kB of memory to work.\n\nThe software was tested under micropython 1.12 (esp32, esp8266).\n\nWhat you can do with this library:\n##################################\n\n* Run any task at precisely defined intervals\n* Delete and add tasks while the program is running.\n* Run the task a certain number of times and many more.\n\nLibrary working principle:\n##########################\n\nGenerally, the working principle is, in a pseudo-code:\n\n.. code-block::\n\n    insert(action_period, action_pointers, 'minute_5s', callback)\n\n    while (the hardware timer is running):\n        current_action_period_pointer = current_time % action_period\n        if current_action_period_pointer in action_pointers:\n            run callback\n\nWe have almost complete freedom to choose the action_period as long as it is greater than zero.\n\nThe action_pointers variable should contain data of \"set\" or \"range\" type.\n\nWhen we want to use irregular points in time we use \"set\". Otherwise, you can use \"range\". This gives us minimal\nmemory overhead and at the same time a lot of freedom in defining when actions are to start.\n\nRequirements:\n#############\n\n* The board on which the micropython is installed(v1.12)\n* The board must have support for hardware timers.\n\nInstall\n#######\nYou can install using the upip:\n\n.. code-block:: python\n\n    import upip\n    upip.install(\"micropython-mcron\")\n\nor\n\n.. code-block:: bash\n\n    micropython -m upip install -p modules micropython-mcron\n\nYou can clone this repository, and install it manually:\n\n.. code-block:: bash\n\n    git clone https://github.com/fizista/micropython-mcron.git\n    cd ./micropython-mcron\n\nSimple examples\n###############\n\nSimple code run every 5 seconds:\n\n.. code-block:: python\n\n    import utime\n    import mcron\n    import mcron.decorators\n\n    c = 0\n\n    def counter(callback_id, current_time, callback_memory):\n        global c\n        c += 1\n        print('call: %s %s' % (c, utime.localtime()))\n\n    mcron.init_timer()\n    mcron.insert(mcron.PERIOD_MINUTE, range(0, mcron.PERIOD_MINUTE, 5), 'minute_5s', counter)\n\nStart the task at exactly 6:30 a.m. and 10:30 p.m. every day.\n\n.. code-block:: python\n\n    mcron.insert(mcron.PERIOD_DAY, {6 * 60 * 60 + 30 * 60, 22 * 60 * 60 + 30 * 60}, 'day_6_30__22_30', callback)\n\nStart the task 4 times a day.\n\n.. code-block:: python\n\n    mcron.insert(mcron.PERIOD_DAY, range(0, mcron.PERIOD_DAY, mcron.PERIOD_DAY // 4), 'day_x4', callback)\n\nStart the task every 11 seconds from now.\n\n.. code-block:: python\n\n    mcron.insert(11, {0}, '11s_now', callback, from_now=True)\n\nStart the task every 11 seconds.\n\n.. code-block:: python\n\n    mcron.insert(11, {0}, '11s', callback)\n\nStart the task successfully three times. Start this task every 5 seconds.\n\n.. code-block:: python\n\n    mcron.insert(\n        mcron.PERIOD_MINUTE, range(0, mcron.PERIOD_MINUTE, 5), 'minute_5s_3x_suc',\n        mcron.decorators.successfully_run_times(3)(lambda *a, **k: utime.time() % 10 == 0)\n    )\n\nStart the task three times. Start this task every 5 seconds.\n\n.. code-block:: python\n\n    mcron.insert(\n        mcron.PERIOD_MINUTE, range(0, mcron.PERIOD_MINUTE, 5), 'minute_5s_3x',\n        mcron.decorators.run_times(3)(callback)\n    )\n\nRemove the action:\n\n.. code-block:: python\n\n    mcron.remove('action_id')\n\nRemove all actions:\n\n.. code-block:: python\n\n    mcron.remove_all()\n\nCapturing action errors is possible by replacing and/or adding your own function.\n\n.. code-block:: python\n\n    def my_exception_processor(e):\n        send_exception_to_server(e)\n        write_exception_to_disk(e)\n\n    mcron.callback_exception_processors.append(my_exception_processor)\n\nImportant notes:\n################\n\n* If the execution time of all tasks is longer than (1000ms - 1.5 * _timer_period), then the TLPTimeException exception\n  is thrown. This tells you that a task is blocking the device, and probably prevents the execution of the next action\n  in the next second. It will be up to the programmer what he will do after intercepting this error. He can do nothing,\n  and he can run the missed tasks.\n* If there are several functions to run at a given time, then they are started without a specific order.\n\nHow to test\n###########\n\nCopy the tests.py file from the https://github.com/fizista/micropython-mcron.git repository to your test board\nand run the command on this device:\n\n.. code-block:: python\n\n    import tests\n\n*******************\nSupport and license\n*******************\n\nIf you have found a mistake or other problem, write in the issues.\n\nIf you need a different license for this library (e.g. commercial),\nplease contact me: fizista+mcron@gmail.com.\n\n\n.. _SimpleCRON: https://github.com/fizista/micropython-scron","funding_links":[],"categories":["Libraries"],"sub_categories":["Scheduling"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffizista%2Fmicropython-mcron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffizista%2Fmicropython-mcron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffizista%2Fmicropython-mcron/lists"}