Get started for free

The following is an excerpt from 'The Infinite Machine'

In a previous article I presented a @debouncedtask

You use @debouncedtask to debounce a function (ie: basically add a smart delay to cope with it being called too often), and run it as a separate task on App Engine. The code might look like this:

debounced-1

That code makes sure that cleanup is only called once every 60 seconds, and is delayed until 10 seconds after the most recent previous invocation.

What I explained briefly, but didn’t go into, was how @debouncedtask knows what to debounce. For instance, say I have two debounced functions:

debounced-2

or what if I want to debounce the same function separately for every value of an argument:

debounced-3

or even if I want to debounce it separately based on a variable in the closure?

debounced-4

@debouncedtask handles all of this transparently. To do this, it needs to generate an id that uniquely identifies a combination of function + closure + args, which it then uses to store timing data in memcache.

It has access to the function object (which includes closure info) and the args & kwargs. We could use the function name and module to identify the function (f.__name__ etc), but this isn’t reliable; the __name__ can be missing or wrong or misleading for many reasons.

Click Here to Read More


Emlyn O'Regan is a Co-Founder and the CTO of xapiapps, and the author of "The Infinite Machine"

THIS EBOOK MIGHT INTEREST YOU

LX-Designer-Book

The LX Designer’s Handbook

A comprehensive guide to the changing world of learning design

Download ebook for free

Comments

Get the latest updates by email