I want to reattempt failing readLines
fetches using tryCatch
. This works as expected, as long as I don't wrap it inside a future.apply::future_lapply
call for processing a list or vector.
The problem can be reproduced using this code:
read_lines_retrying <- function(url, attempts = 5, throttle = 5) {
result <- NA
while (is.na(result) && 0 < attempts) {
attempts <- attempts - 1
result <- tryCatch(
{
readLines(url)
},
error = function(cond) {
message("caught error:")
message(cond)
message("")
Sys.sleep(throttle)
return(NA)
}
)
}
if (is.na(result)) {
stop(paste("could not get URL ", url))
}
return(result)
}
urls <- c("http://nonexistant.nonexistant")
future.apply::future_lapply(urls, read_lines_retrying)
Of course, the code is meant to retry on transient readLines
failures, while the example URL will always fail, but this way problem can be most easily seen. When using lapply
instead of future.apply::future_lapply
, it takes at least 5 seconds to complete because it waits 5 seconds after each of the 5 attempts. This in not the case with future.apply::future_lapply
, demonstrating that the exception handling doesn't work.
What am I doing wrong, and how can I get tryCatch
to work inside future.apply::future_lapply
?