3 from __future__ import with_statement
13 Make a function mainloop friendly. the function will be called at the
14 next mainloop idle state.
17 >>> misc.validate_decorator(async)
20 @functools.wraps(func)
21 def new_function(*args, **kwargs):
27 gobject.idle_add(async_function)
32 def throttled(minDelay, queue):
34 Throttle the calls to a function by queueing all the calls that happen
35 before the minimum delay
39 >>> misc.validate_decorator(throttled(0, Queue.Queue()))
42 def actual_decorator(func):
48 func, args, kwargs = queue.pop(0)
49 lastCallTime[0] = time.time() * 1000
53 @functools.wraps(func)
54 def new_function(*args, **kwargs):
55 now = time.time() * 1000
57 lastCallTime[0] is None or
58 (now - lastCallTime >= minDelay)
63 queue.append((func, args, kwargs))
64 lastCallDelta = now - lastCallTime[0]
65 processQueueTimeout = int(minDelay * len(queue) - lastCallDelta)
66 gobject.timeout_add(processQueueTimeout, process_queue)
70 return actual_decorator