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

best practice re: closing the connection? #48

Closed
jennybc opened this issue Nov 29, 2015 · 2 comments
Closed

best practice re: closing the connection? #48

jennybc opened this issue Nov 29, 2015 · 2 comments

Comments

@jennybc
Copy link
Contributor

jennybc commented Nov 29, 2015

I am using curl() following usage I see in the official examples, like so:

x <- readLines(curl(SOME_URL)

but inside a loop over SOME_URL. I then got lots of warnings about closing unused connections. Am I supposed to be more careful and always store then close the connection? Does it even matter?

con <- curl(SOME_URL)
x <- readLines(con)
close(con)
@jeroen
Copy link
Owner

jeroen commented Nov 30, 2015

It is good practice to close() the connection. If you forget to, the garbage collector closes the connection when it cleans up the con object (with a warning). This holds for all connection types by the way (url(), file(), socket() etc), not just curl(). See also this topic.

Within a function, it is common to use the on.exit finalizer for housekeeping. The benefit of on.exit is that it also runs if the function exits with an error, so you can be sure the connection always gets closed.

do_something <- function(SOME_URL){
  con <- curl(SOME_URL, open = "r")
  on.exit(close(con))
  readLines(con)
}

@jennybc
Copy link
Contributor Author

jennybc commented Nov 30, 2015

Thanks that is helpful. Yes I had already decided to properly close the connections because the sheer volume of warnings was alarming. Good tip re: on.exit().

@jennybc jennybc closed this as completed Nov 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants