limit_rate: Limit the rate at which a function will execute

Description Usage Arguments Value See Also Examples

Description

Limit the rate at which a function will execute

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
limit_rate(f, ..., precision = 60)

## S3 method for class 'list'
limit_rate(f, ..., precision = 60)

## S3 method for class 'function_list'
limit_rate(f, ..., precision = 60)

## S3 method for class 'function'
limit_rate(f, ..., precision = 60)

Arguments

f

A single function to be rate-limited, or a named list of functions

...

One or more rates, created using rate

precision

The precision with which time intervals can be measured, in hertz

Value

If f is a single function, then a new function with the same signature and (eventual) behavior as the original function, but rate limited. If f is a named list of functions, then a new list of functions with the same names and signatures, but collectively bound by a shared rate limit.

See Also

rate, UPDATE_RATE

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## limiting a single function
f <- limit_rate(Sys.time, rate(n = 5, period = .1))
res <- replicate(10, f())
## show the elapsed time between each function call:
round(res[-1] - head(res, -1), 3)

## for multiple functions, make sure the list is named:
f <- function() 1
g <- function() 2
limited <- limit_rate(list(f = f, g = g), rate(n = 1, period = .1))
system.time({limited$f(); limited$g()})

ratelimitr documentation built on May 2, 2019, 6:53 a.m.