Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks run too often #3

Closed
trideeindhoven opened this issue Jun 13, 2021 · 3 comments
Closed

Tasks run too often #3

trideeindhoven opened this issue Jun 13, 2021 · 3 comments

Comments

@trideeindhoven
Copy link

I seem to be running into a problem with your library. I am running this on an ESP32 and the tasks fire WAY too quickly:

mcron.init_timer()
mcron.insert(mcron.PERIOD_MINUTE, range(0, mcron.PERIOD_MINUTE, 10), 'minute_10s', run_task())

I expect run_task() to run every 10 seconds, but it looks like it runs faster than every 100ms.
I am sure i am misconfiguring something here, but as far as i can see mcron is using the hardware esp timers, so it is not something like the clockspeed.
I hope you can help me. Thank you!

Device board: ESP32
Micropython version: 1.11

@fizista
Copy link
Owner

fizista commented Jun 14, 2021

MCRON is not suitable for running tasks with millisecond accuracy. Of course, you can increase the accuracy by using the timer_period parameter in the init_timer function.

You have to assume that the task start time depends on many factors:

  • when the job was created (insert method).
  • whether the countdown should start while the task is being created e.g. insert(... , from_now=True)
  • the period of time for checking the conditions of calling tasks init_timer(... , timer_period=250)
  • number of tasks running at the same time
  • tasks called earlier, which will not manage to finish before calling other tasks

@trideeindhoven
Copy link
Author

Hello fizista,

Thank you for your reply. I am not looking for ms accuracy. What i am trying to say is that my task SHOULD run every 10sec, but is now running every ~50-100ms. So the task is running very often. I have commented out all other tasks, so i know this is the only task running.

@trideeindhoven
Copy link
Author

As usually the solution was something simple. Documenting here for other:

the function run_task() used to be a while True: with a sleep() in it. I removed the sleep() but obviously forgot to remove the while True: at the top of this large(ish) function. So it now works perfectly.
Also: i was using def run_task():, but you need to use def run_task(callback_id, current_time, callback_memory): (exactly like the example, but this is important for your program to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants