From 11d6db9d685f1174fc5bfe5abdb5c409bb94f8a4 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sun, 25 Nov 2018 10:56:15 +0000 Subject: [PATCH] Add timeout sample. Remove make axtls from buildnew script. --- README.md | 3 ++- fastbuild/buildnew | 1 - timed_function/timeout.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 timed_function/timeout.py 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) +