[R] Too many open files

Seth Falcon sfalcon at fhcrc.org
Sat May 27 01:21:36 CEST 2006


"Omar Lakkis" <uofiowa at gmail.com> writes:

> This may be more of an OS question ...
> I have this call
>
> r = get.hist.quote(symbol, start= format(start, "%Y-%m-%d"), end=
> format(end, "%Y-%m-%d"))
> which does a url request
>
> in a loop and my program runs out of file handlers after few hundred
> rotations. The error message is:  'Too many open files'. Other than
> increasing the file handlers assigned to my process, is there a way to
> cleanly release and reuse these connections?

Inside your loop you need to close the connection object created by
url().

for (i in 1:500) {
    con <- url(urls[i])
    ## ... stuff here ...
    close(con)
}

R only allows you to have a fixed number of open connections at one
time, and they do not get closed automatically when they go out of
scope.  These commands may help make clear how things work...

> showConnections()
     description class mode text isopen can read can write
> f = url("http://www.r-project.org", open="r")
> showConnections()
  description                class mode text   isopen   can read can write
3 "http://www.r-project.org" "url" "r"  "text" "opened" "yes"    "no"     
> rm(f)
> showConnections()
  description                class mode text   isopen   can read can write
3 "http://www.r-project.org" "url" "r"  "text" "opened" "yes"    "no"     
> f <- getConnection(3)
> close(f)
> f
Error in summary.connection(x) : invalid connection
> showConnections()
     description class mode text isopen can read can write
> 

+ seth



More information about the R-help mailing list