diff --git a/README.md b/README.md index 0615510..d6e49e4 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,8 @@ Raise an exception if a firmware build is earlier than a given date. # timed_function -Time a function's execution using a decorator. +Time a function's execution using a decorator. Also a way to implement timeouts +using a closure. # ESP8266 (MQTT benchmark) diff --git a/fastbuild/buildnew b/fastbuild/buildnew index 257ae95..b4cda60 100755 --- a/fastbuild/buildnew +++ b/fastbuild/buildnew @@ -17,7 +17,6 @@ cd ../esp8266 make clean cd ../unix make clean -make -j 8 axtls # If you're going to enable deplibs: see micropython/README #make deplibs make -j 8 diff --git a/timed_function/timeout.py b/timed_function/timeout.py new file mode 100644 index 0000000..6788a74 --- /dev/null +++ b/timed_function/timeout.py @@ -0,0 +1,15 @@ +# Implement a timeout using a closure +import utime + +def to(t): + tstart = utime.ticks_ms() + def foo(): + return utime.ticks_diff(utime.ticks_ms(), tstart) > t + return foo + +# Usage +t = to(3000) +for _ in range(10): + print(t()) + utime.sleep(0.5) +