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:


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:


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


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


@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"

Start Your Free Trial

Get the latest updates by email