[Haskell-cafe] Lazy IO and closing of file handles

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Mar 14 18:27:45 EDT 2007

> When using readFile to process a large number of files, I am exceeding
> the resource limits for the maximum number of open file descriptors on
> my system.  How can I enhance my program to deal with this situation
> without making significant changes?

Read in data strictly, and there are two obvious ways to do that:

    -- Via strings:

    readFileStrict f = do
        s <- readFile f
        length s `seq` return s

    -- Via ByteStrings
    readFileStrict  = Data.ByteString.readFile
    readFileStrictString  = liftM Data.ByteString.unpack Data.ByteString.readFile

If you're reading more than say, 100k of data, I'd use strict
ByteStrings without hesitation. More than 10M, and I'd use lazy

-- Don

More information about the Haskell-Cafe mailing list