writing a Linux and macOS Callback Timer. #15981
Replies: 1 comment 8 replies
-
Hi, I've needed a "mock timer" on unix a few times, generally to unit test code that normally runs on hardware. Lately I've just used an async task for this as if you're careful with async functions to not block badly you should be able to easily get reponsive timing (especially on unix) but they are easy to break if a different async task blocks unexpectedly. Previously I've written a unix class Timer:
def __init__(self, timer_id):
self.id = timer_id
self._callback = None
self._freq = None
self._stop = False
import _thread
_thread.start_new_thread(self._timer, ())
def init(self, freq, callback):
self._callback = callback
self._freq = freq
def deinit(self):
self._stop = True
def _timer(self):
while True:
if self._stop:
break
if self._freq is None:
continue
if self._callback:
try:
micropython.schedule(lambda _: self._callback(), None)
except Exception as ex:
import sys
sys.print_exception(ex)
utime.sleep_ms(int(self._freq * 1000)) This isn't directly answering why your scheduled functions aren't working though, that's a tricky one. When referencing the C snipped you link, have you used those ENTER/EXIT defines and ensured they are actually defined properly to the atomic guard functions? Ensuring C thread safety is a big part of the complexity here. Separately, what's your python application doing during this time? scheduled functions aren't pre-emptive as such, the scheduler is serviced during times when the VM is "idle". This is typically during a time.sleep, time.idle, async sleep, stream read etc. In C it's whenever |
Beta Was this translation helpful? Give feedback.
-
In a project I am working on there is a need to have a periodic callback timer and that timer needs to work on both Linux and also on macOS. The timer needs to be semi accurate +- 5ms or so. This functionality is not available in the "unix" port and I have been trying various ways to add it.
I cannot use
create_timer
from the stdlib because macOS doesn't have this function available. I have attempted to use 'dispatch_source_set_timer' for macOS but I did not have any luck with it working.I am using SDL2 in my project as well and I realized that this has the functionality I am looking for. It works partially. It makes the callback to a C function when it is supposed to without any problems. The problems occur when calling the python callback function associated with the timer. Now I know that the C callback is called from a different thread and I have used the code located here
micropython/ports/unix/mpbthciport.c
Line 97 in 338df1a
as an example of how to call a python function from a different thread. The callback to the python function is never made but the callback to the C function that schedules the python function callback is made.
I have tried calling the python function directly using mp_sched_schedule and also calling a C function like what is done in the link above and neither the C function nor the python function gets called. It is like the scheduler is not working like it should. No errors are seen at all.
can anyone shed any light as to why the function that is supplied the MicroPython scheduler never gets called?
Beta Was this translation helpful? Give feedback.
All reactions