Search code examples
rpause

Interrupting code after given amount of iterations with future_lapply


I tried for some hours now to fix this problem, but I did not find a solution until now. The problem is the following: I have the following peace of code, where I basically just check whether a URL mentioned in a data base is updated or not. Every 10 iterations I would like the code to be interrupted for half a second for 10 iterations. Thus, i=1:9 run without a break, 10:19 would have a small pause and 20:29 run normally...

n <- 100
API_expired<- logical(n) 
websites <- organizations$properties.api_path[1:n]
Updated <- function(x){is.null(crunchbase_GET(x))}
Updated_pause <- function(x){is.null(crunchbase_GET(x))
                      sys.sleep(0.5}
cl <- makeCluster(detectCores(), type = "PSOCK")
plan(cluster, workers = cl)

API_expired[1:n]<-  unlist(future_lapply(websites[1:n]))
stopCluster(cl)

Solution

  • You could use

    if (i %/% 10 %% 2 == 1) { Sys.sleep(0.5) }
    

    to identify the 10:19, 30:39, ... ranges.