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:
lazy val clock = Enumerator.fromCallback { () =>
Promise.timeout(Some(dateFormat.format(new Date)), 100 milliseconds)
}
Then it is used like this:
Ok.stream(clock &> Comet(callback = "parent.clockChanged"))
Which will feed the result to the client in chunks. The Enumerator
object also has a fromFile
, fromStream
(as in java.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:
- you can feed an enumator andThen another
- you can map a function of type
(T) => U
over an enumerator ofT
to get an enumerator ofU
- you can interleave two enumerators
- an iteratee can leave some input to be consumed by another iteratee
来源:https://stackoverflow.com/questions/10177666/cant-understand-iteratee-enumerator-enumeratee-in-play-2-0