I have just started to learn the Play 2.0 Framework. The one thing I just can't understand is the Iteratee, Enumerator and Enumeratee pattern described in the play tutorial. I have very little experience in functional languages.
What does this pattern accomplish?
How does it help me write non-blocking/reactive code?
some simple examples would help.
The playframework 2.0 download comes with some samples. Two of which have Iteratee/Comet examples. For instance, the comet-clock sample app shows:
Then it is used like this:
Which will feed the result to the client in chunks. The
Enumerator
object also has afromFile
,fromStream
(as injava.io.InputStream
) utility enumerator functions.I am not sure where this is done but the assumption is that this chunked processing is not tying up threads. It would be very interesting to see some benchmarks, as there is certainly overhead in the implementation of iteratees as the data to be processed as well as the computation is wrapped in various objects.
Data that is fed from an enumerator is wrapped so that it can indicates there is more data to process or the data has reached the end (EOF). Processing results of iteratees are also wrapped so that it can indicate whether a result has been computed on some input or more input is needed to compute a result. I recommend John De Goes' nescala presentation that shows the evolution from a fold to Iteratees. Edit: Brendan McAdams has a nice Scala Days 2012 presentation on Async and non-blocking - towards the end of the presentation (~26min) it touches on iteratees and how it helps with processing database cursor style IO in async style.
One touted benefit of
Iteratees
is that they compose. Here are a few ways they compose:(T) => U
over an enumerator ofT
to get an enumerator ofU