opentripplanner icon indicating copy to clipboard operation
opentripplanner copied to clipboard

Implement scopes to speed up routing

Open mem48 opened this issue 2 years ago • 1 comments

https://github.com/jeroen/curl/issues/293

mem48 avatar Feb 21 '23 23:02 mem48

otp_async <- function(urls, host_con, id){

  # Success Function
  otp_success <- function(res){
    p()
    data <<- c(data, list(rawToChar(res$content)))
    urls2 <<- c(urls2, res$url)
  }
  # Fail Function
  otp_failure <- function(msg){
    p()
    cat("Error: ", msg, "\n")
    urls2 <<- c(urls2, msg$url)
  }

  t1 <- Sys.time()

  pool <- curl::new_pool(host_con = host_con)
  data <- list()
  urls2 <- list()

  for(i in seq_len(length(urls))){
    h <- make_handle(i)
    curl::curl_fetch_multi(urls[i],
                           otp_success,
                           otp_failure ,
                           pool = pool,
                           handle = h)
  }
  message(Sys.time()," sending ",length(urls)," routes requests using ",host_con," threads")
  p <- progressr::progressor(length(urls))
  out <- curl::multi_run(timeout = Inf, pool = pool)
  urls2 <- unlist(urls2)
  data <- data[match(urls, urls2)]
  t2 <- Sys.time()
  message("Done in ",round(difftime(t2,t1, units = "mins"),1)," mins")
  return(unlist(data, use.names = FALSE))
}

make_handle <- function(x){
  handle <- curl::new_handle()
  curl::handle_setopt(handle, copypostfields = paste0("routeid=", x))
  return(handle)
}

mem48 avatar Mar 15 '23 16:03 mem48