[Haskell-cafe] Exception: Too many open files

Bas van Dijk basvandijk at home.nl
Mon Oct 23 14:48:24 EDT 2006


Hello Haskellers,

I'm wondering how to get the following to work:

I need to parse about 18.000 files. I would like to have a 
function 'parseFiles' that parses all these files and returns the result in a 
list.

When I execute 'test' from the simplified code below I get the error:

*** Exception: file_0014994.txt: openFile: resource exhausted (Too many open 
files)

So it seems that 'parseFiles' tries to open all the ~18.000 files and gets 
exhausted when opening the 14994 file.

What I would like is 'take 3 =<< parseFiles' to read only the first 3 files.

Is this possible, and if so, what is the best way to do this?

Note that the code below is a bit simplified:

------------------------------------------------------------------------------
module Main where

import Text.ParserCombinators.Parsec

data T = ...

test = print . take 3 =<< parseFiles

parseFiles :: IO [T]
parseFiles = mapM parseFile =<< getFileFPs

getFileFPs :: IO [FilePath]
getFileFPs = ... -- returns a large list of about 18.000 FilePaths

parseFile ::  FilePath -> IO T
parseFile fp = liftM getRight $ parseFromFile someParser fp

getRight (Right r) = r

someParser :: Parser T
someParser = ...
------------------------------------------------------------------------------

Greetings,

Bas van Dijk



More information about the Haskell-Cafe mailing list