Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slowly() adverb #389

Closed
jennybc opened this issue Oct 3, 2017 · 3 comments
Closed

Add slowly() adverb #389

jennybc opened this issue Oct 3, 2017 · 3 comments
Labels
adverb 📚 feature a feature request or enhancement

Comments

@jennybc
Copy link
Member

jennybc commented Oct 3, 2017

I recently used safely() to wrap gmailr::send_message(), which I then used with pmap() to iterate over rows = recipients. I ran into sporadic rate limit problems.

It would be nice to also have a slowly() adverb for adding a little bit of delay to each call.

@DavisVaughan
Copy link
Member

I like this idea!

library(purrr)

slowly <- function(.f, delay = 1, otherwise, quiet = TRUE) {
  .f <- as_mapper(.f)
  function(...) {
    ret <- purrr:::capture_error(.f(...), otherwise, quiet)
    Sys.sleep(delay)
    ret
  }
}

slow_log <- slowly(log)

timeit <- proc.time()

list(2, 10, 100) %>%
  map(slow_log)
#> [[1]]
#> [[1]]$result
#> [1] 0.6931472
#> 
#> [[1]]$error
#> NULL
#> 
#> 
#> [[2]]
#> [[2]]$result
#> [1] 2.302585
#> 
#> [[2]]$error
#> NULL
#> 
#> 
#> [[3]]
#> [[3]]$result
#> [1] 4.60517
#> 
#> [[3]]$error
#> NULL

proc.time() - timeit
#>    user  system elapsed 
#>   0.005   0.000   3.012

@hadley hadley added the feature a feature request or enhancement label Feb 5, 2018
@AshesITR
Copy link
Contributor

AshesITR commented Feb 10, 2018

Related: https://github.com/tarakc02/ratelimitr
Maybe it's better to specify the maximum rate in such a fashion?

Reprex using ratelimitr:

library(purrr)
library(ratelimitr)

slow_log <- limit_rate(log, rate(1, 1))  # 1 run / 1 second

system.time({
  c(2, 10, 100) %>% map(slow_log)
})
#>        User      System verstrichen 
#>        0.02        0.00        2.09

As you can see, no unnecessary waiting after the last call has been made :)

@lionel-
Copy link
Member

lionel- commented Nov 16, 2018

Should be made consistent with #333

lionel- added a commit to lionel-/lowliner that referenced this issue Dec 12, 2018
lionel- added a commit to lionel-/lowliner that referenced this issue Dec 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adverb 📚 feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants