Skip to content

multimer

multimer

multimer

Cross-platform Timer class for *Python.

Enables using 'from multimer import Timer' on MicroPython on microcontrollers, on MicroPython on Unix (which doesn't have a machine.Timer) and CPython (ditto).

_ffi.py uses MicroPython ffi to connect to libc and librt (MicroPython unix only). MicroPython Windows and other ports without ffi or threads use _polling.py. CPython on Linux uses _ctypes.py (POSIX librt via ctypes; callbacks on the main thread without run_queued). Other CPython ports use _threading.py (_sdl2.py if threading is unavailable). CircuitPython unix uses _threading.py.

Returns None if the platform is not supported rather than raising an ImportError so that the client can handle the error more gracefully (e.g. by using if Timer is not None:).

Usage

from multimer import Timer, schedule, run_queued, ticks_ms, ticks_diff tim = Timer() tim.init(mode=Timer.PERIODIC, period=500, callback=lambda t: print(".")) .... tim.deinit()

On CPython (non-Linux), CircuitPython, and MicroPython ports using the polling backend, call run_queued() from the main thread to drain queued callbacks (for example in an event loop). sleep_ms() also advances polling timers.

For asyncio-based apps, use multimer.aio — see docs/concepts/multimer.md.

Classes

Functions:

get_timer

get_timer(callback, period=33, *, asynchronous=None, warn=True)

Creates and returns a timer to periodically call the callback function

Parameters:

Name Type Description Default
callback function

The function to call periodically

required
period int

The period in milliseconds, default is 33ms (30fps)

33
asynchronous bool

If True, use multimer.aio.Timer. If None or False, use the default Timer loaded at import.

None
warn bool

If True and this platform requires run_queued(), print a reminder. Defaults to True.

True