def rate_limiter(max_per_second):
 min_interval = (1.0 / float(max_per_second))
 last_time_called = [0.0]
 def throttler():
  elapsed = (time.clock() - last_time_called[0])
  left_to_wait = (min_interval - elapsed)
  if (left_to_wait > 0):
   time.sleep(left_to_wait)
  last_time_called[0] = time.clock()
 return throttler

def rate_limiter ( max_per_second ) : min_interval = ( 1.0 / float ( max_per_second ) ) last_time_called = [ 0.0 ] def throttler ( ) : elapsed = ( time . clock ( ) - last_time_called [ 0 ] ) left_to_wait = ( min_interval - elapsed ) if ( left_to_wait > 0 ) : time . sleep ( left_to_wait ) last_time_called [ 0 ] = time . clock ( ) return throttler
limit number of calls to returned closure per second to max_per_second algorithm adapted from here: URL .