How to properly close the connection in R, so its "slot" connection will be released?

I use readLines (text url) in a script where readLines (text url) is called several hundred times, where each text url is unique.

After about 125 calls to readLines (text url), I got an error, "all connections are in use."

When I check my open connections with showConnections (all = TRUE), for URL connections I see:

description class ... isopen "www.site.com" "url" ... "closed" ... 

How to remove these closed connections from environment R so that I can open new connections?

Also, I tried opening the URLs before passing, passing the url connection to readLines, and then closing the connection after the connection was completed, and still encountering the same problem.

+6
source share
1 answer

The easiest way to avoid such problems is to explicitly close the connection when you are done with it. In R, the easiest way to do this is to use on.exit() , which ensures that the URL is closed even if an error occurs in your code

 read_url <- function(url, ...) { on.exit(close(url)) readLines(url, ...) } showConnections() g <- read_url("http://www.google.com") showConnections() 
+5
source

Source: https://habr.com/ru/post/958708/


All Articles