[Haskell-cafe] Need some advice around lazy IO

Dan Doel dan.doel at gmail.com
Mon Mar 18 17:06:53 CET 2013


Do note that deepSeq alone won't (I think) change anything in your
current code. bug will deepSeq the file contents. And the cons will
seq bug. But nothing is evaluating the cons. And further, the cons
isn't seqing the tail, so none of that will collapse, either. So the
file descriptors will still all be opened at once.

Probably the best solution if you choose to go this way is:

    bug <- evaluate (fileContents2Bug $!! str)

which ties the evaluation of the file contents into the IO execution.
At that point, deepSeqing the file is probably unnecessary, though,
because evaluating the bug will likely allow the file contents to be
collected.

On Mon, Mar 18, 2013 at 6:42 AM, C K Kashyap <ckkashyap at gmail.com> wrote:
> Thanks Konstantin ... I'll try that out too...
>
>
>
> Regards,
> Kashyap
>
>
> On Mon, Mar 18, 2013 at 3:31 PM, Konstantin Litvinenko
> <to.darkangel at gmail.com> wrote:
>>
>> On 03/17/2013 07:08 AM, C K Kashyap wrote:
>>>
>>> I am working on an automation that periodically fetches bug data from
>>> our bug tracking system and creates static HTML reports. Things worked
>>> fine when the bugs were in the order of 200 or so. Now I am trying to
>>> run it against 3000 bugs and suddenly I see things like - too  many open
>>> handles, out of memory etc ...
>>>
>>> Here's the code snippet - http://hpaste.org/84197
>>>
>>> It's a small snippet and I've put in the comments stating how I run into
>>> "out of file handles" or simply file not getting read due to lazy IO.
>>>
>>> I realize that putting ($!) using a trial/error approach is going to be
>>> futile. I'd appreciate some pointers into the tools I could use to get
>>> some idea of which expressions are building up huge thunks.
>>
>>
>> You problem is in
>>
>> let bug = ($!) fileContents2Bug str
>>
>> ($!) evaluate only WHNF and you need NF. Above just evaluate to first char
>> in a file, not to all content. To fully evaluate 'str' you need something
>> like
>>
>> let bug = Control.DeepSeq.rnf str `seq` fileContents2Bug str
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list