Reactive-Stream不只是简单的push-model-stream, 它还带有“拖式”(pull-model)性质。这是因为在Iteratee模式里虽然理论上由Enumerator负责主动推送数据,实现了push-model功能。但实际上Iteratee也会根据自身情况,通过提供callback函数通知Enumerator可以开始推送数据,这从某种程度上也算是一种pull-model。换句话讲Reactive-Streams是通过push-pull-model来实现上下游Enumerator和Iteratee之间互动的。我们先看个简单的Iteratee例子:

def showElements: Iteratee[Int,Unit] = Cont {
case Input.El(e) =>
println(s"EL($e)")
showElements
case Input.Empty => showElements
case Input.EOF =>
println("EOF")
Done((),Input.EOF)
} //> showElements: => play.api.libs.iteratee.Iteratee[Int,Unit]
val enumNumbers = Enumerator(,,,,) //> enumNumbers : play.api.libs.iteratee.Enumerator[Int] = play.api.libs.iteratee.Enumerator$$anon$19@47f6473 enumNumbers |>> showElements //> EL(1)
//| EL(2)
//| EL(3)
//| EL(4)
//| EL(5)
//| res0: scala.concurrent.Future[play.api.libs.iteratee.Iteratee[Int,Unit]] = Success(Cont(<function1>))

我们看到:enumNumbers |>> showElements立刻启动了运算。但并没有实际完成数据发送,因为showElements并没有收到Input.EOF。首先,我们必须用Iteratee.run来完成运算:

val it = Iteratee.flatten(enum |>> consumeAll).run//> El(1)
//| El(2)
//| El(3)
//| El(4)
//| El(5)
//| El(6)
//| El(7)
//| El(8)
//| EOF
//| it : scala.concurrent.Future[Int] = Success(99)

这个run函数是这样定义的:

/**
* Extracts the computed result of the Iteratee pushing an Input.EOF if necessary
* Extracts the computed result of the Iteratee, pushing an Input.EOF first
* if the Iteratee is in the [[play.api.libs.iteratee.Cont]] state.
* In case of error, an exception may be thrown synchronously or may
* be used to complete the returned Promise; this indeterminate behavior
* is inherited from fold().
*
* @return a [[scala.concurrent.Future]] of the eventually computed result
*/
def run: Future[A] = fold({
case Step.Done(a, _) => Future.successful(a)
case Step.Cont(k) => k(Input.EOF).fold({
case Step.Done(a1, _) => Future.successful(a1)
case Step.Cont(_) => sys.error("diverging iteratee after Input.EOF")
case Step.Error(msg, e) => sys.error(msg)
})(dec)
case Step.Error(msg, e) => sys.error(msg)
})(dec)

再一个问题是:enumNumbers |>> showElements是个封闭的运算,我们无法逐部分截取数据流,只能取得整个运算结果。也就是说如果我们希望把一个Enumerator产生的数据引导到fs2 Stream的话,只能在所有数据都读入内存后才能实现了。这样就违背了使用Reactive-Streams的意愿。那我们应该怎么办?一个可行的方法是使用一个存储数据结构,用两个线程,一个线程里Iteratee把当前数据存入数据结构,另一个线程里fs2把数据取出来。fs2.async.mutable包提供了个Queue类型,我们可以用这个Queue结构来作为Iteratee与fs2之间的管道:Iteratee从一头把数据压进去(enqueue),fs2从另一头把数据取出来(dequeue)。

我们先设计enqueue部分,这部分是在Iteratee里进行的:

def enqueueTofs2(q: async.mutable.Queue[Task,Option[Int]]): Iteratee[Int,Unit] = Cont {
case Input.EOF =>
q.enqueue1(None).unsafeRun
Done((),Input.EOF)
case Input.Empty => enqueueTofs2(q)
case Input.El(e) =>
q.enqueue1(Some(e)).unsafeRun
enqueueTofs2(q)
} //> enqueueTofs2: (q: fs2.async.mutable.Queue[fs2.Task,Option[Int]])play.api.libs.iteratee.Iteratee[Int,Unit]

先分析一下这个Iteratee:我们直接把enqueueTofs2放入Cont状态,也就是等待接受数据状态。当收到数据时运行q.enqueue1把数据塞入q,然后不断循环运行至收到Input.EOF。注意:q.enqueue1(Some(e)).unsafeRun是个同步运算,在未成功完成数据enqueue1的情况下会一直占用线程。所以,q另一端的dequeue部分必须是在另一个线程里运行,否则会造成整个程序的死锁。fs2的Queue类型款式是:Queue[F,A],所以我们必须用Stream.eval来对这个Queue进行函数式的操作:

val fs2Stream: Stream[Task,Int] = Stream.eval(async.boundedQueue[Task,Option[Int]]()).flatMap { q =>
//run Enumerator-Iteratee and enqueue data in thread 1
//dequeue data and en-stream in thread 2(current thread)
}

因为Stream.eval运算结果是Stream[Task,Int],所以我们可以得出这个flatMap内的函数款式 Queue[Task,Option[Int]] => Stream[Task,Int]。下面我们先考虑如何实现数据enqueue部分:这部分是通过Iteratee的运算过程产生的。我们提到过这部分必须在另一个线程里运行,所以可以用Task来选定另一线程如下:

    Task { Iteratee.flatten(enumerator |>> pushData(q)).run }.unsafeRunAsyncFuture()

现在这个Task就在后面另一个线程里自己去运算了。但它的运行进展则会依赖于另一个线程中dequeue数据的进展。我们先看看fs2提供的两个函数款式:

/** Repeatedly calls `dequeue1` forever. */
def dequeue: Stream[F, A] = Stream.bracket(cancellableDequeue1)(d => Stream.eval(d._1), d => d._2).repeat /**
* Halts the input stream at the first `None`.
*
* @example {{{
* scala> Stream[Pure, Option[Int]](Some(1), Some(2), None, Some(3), None).unNoneTerminate.toList
* res0: List[Int] = List(1, 2)
* }}}
*/
def unNoneTerminate[F[_],I]: Pipe[F,Option[I],I] =
_ repeatPull { _.receive {
case (hd, tl) =>
val out = Chunk.indexedSeq(hd.toVector.takeWhile { _.isDefined }.collect { case Some(i) => i })
if (out.size == hd.size) Pull.output(out) as tl
else if (out.isEmpty) Pull.done
else Pull.output(out) >> Pull.done
}}

刚好,dequeue产生Stream[F,A]。而unNoneTerminate可以根据Stream(None)来终止运算。现在我们可以把这个Reactive-Streams到fs2-pull-streams转换过程这样来定义:

implicit val strat = Strategy.fromFixedDaemonPool()
//> strat : fs2.Strategy = Strategy
val fs2Stream: Stream[Task,Int] = Stream.eval(async.boundedQueue[Task,Option[Int]]()).flatMap { q =>
Task(Iteratee.flatten(enumNumbers |>> enqueueTofs2(q)).run).unsafeRunAsyncFuture
pipe.unNoneTerminate(q.dequeue)
} //> fs2Stream : fs2.Stream[fs2.Task,Int] = attemptEval(Task).flatMap(<function1>).flatMap(<function1>)

现在这个stream应该已经变成fs2.Stream[Task,Int]了。我们可以用前面的log函数来试运行一下:

def log[A](prompt: String): Pipe[Task,A,A] =
_.evalMap {row => Task.delay{ println(s"$prompt> $row"); row }}
//> log: [A](prompt: String)fs2.Pipe[fs2.Task,A,A] fs2Stream.through(log("")).run.unsafeRun //> > 1
//| > 2
//| > 3
//| > 4
//| > 5

我们成功的把Iteratee的Reactive-Stream转化成fs2的Pull-Model-Stream。

下面是这次讨论的源代码:

import play.api.libs.iteratee._
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.collection.mutable._
import fs2._
object iteratees {
def showElements: Iteratee[Int,Unit] = Cont {
case Input.El(e) =>
println(s"EL($e)")
showElements
case Input.Empty => showElements
case Input.EOF =>
println("EOF")
Done((),Input.EOF)
}
val enumNumbers = Enumerator(,,,,) enumNumbers |>> showElements Iteratee.flatten(enumNumbers |>> showElements).run def enqueueTofs2(q: async.mutable.Queue[Task,Option[Int]]): Iteratee[Int,Unit] = Cont {
case Input.EOF =>
q.enqueue1(None).unsafeRun
Done((),Input.EOF)
case Input.Empty => enqueueTofs2(q)
case Input.El(e) =>
q.enqueue1(Some(e)).unsafeRun
enqueueTofs2(q)
}
implicit val strat = Strategy.fromFixedDaemonPool()
val fs2Stream: Stream[Task,Int] = Stream.eval(async.boundedQueue[Task,Option[Int]]()).flatMap { q =>
Task(Iteratee.flatten(enumNumbers |>> enqueueTofs2(q)).run).unsafeRunAsyncFuture
pipe.unNoneTerminate(q.dequeue)
} def log[A](prompt: String): Pipe[Task,A,A] =
_.evalMap {row => Task.delay{ println(s"$prompt> $row"); row }} fs2Stream.through(log("")).run.unsafeRun }

FunDA(7)- Reactive Streams to fs2 Pull Streams的更多相关文章

  1. FunDA(5)- Reactive Streams:Play with Iteratees

    FunDA的设计目标就是把后台数据库中的数据搬到内存里,然后进行包括并行运算的数据处理,最后可能再对后台数据库进行更新.如果需要把数据搬到内存的话,那我们就必须考虑内存是否能一次性容纳所有的数据,有必 ...

  2. FunDA(6)- Reactive Streams:Play with Iteratees、Enumerator and Enumeratees

    在上一节我们介绍了Iteratee.它的功能是消耗从一些数据源推送过来的数据元素,不同的数据消耗方式代表了不同功能的Iteratee.所谓的数据源就是我们这节要讨论的Enumerator.Enumer ...

  3. FunDA(9)- Stream Source:reactive data streams

    上篇我们讨论了静态数据源(Static Source, snapshot).这种方式只能在预知数据规模有限的情况下使用,对于超大型的数据库表也可以说是不安全的资源使用方式.Slick3.x已经增加了支 ...

  4. FunDA(14)- 示范:并行运算,并行数据库读取 - parallel data loading

    FunDA的并行数据库读取功能是指在多个线程中同时对多个独立的数据源进行读取.这些独立的数据源可以是在不同服务器上的数据库表,又或者把一个数据库表分成几个独立部分形成的独立数据源.当然,并行读取的最终 ...

  5. FunDA(8)- Static Source:保证资源使用安全 - Resource Safety

    我们在前面用了许多章节来讨论如何把数据从后台数据库中搬到内存,然后进行逐行操作运算.我们选定的解决方案是把后台数据转换成内存中的数据流.无论在打开数据库表或从数据库读取数据等环节都涉及到对数据库表这项 ...

  6. FunDA(0)- Functional Data Access accessible to all

    大数据.多核CPU驱动了函数式编程模式的兴起.因为函数式编程更适合多线程.复杂.安全的大型软件编程.但是,对许多有应用软件开发经验的编程者来说,函数式编程模式是一种全新的.甚至抽象的概念,可能需要很长 ...

  7. FunDA(3)- 流动数据行操作:FDAPipeLine operations using scalaz-stream-fs2

    在上节讨论里我们介绍了数据行流式操作的设想,主要目的是把后台数据库的数据载入前端内存再拆分为强类型的数据行,这样我们可以对每行数据进行使用和处理.形象点描述就是对内存里的一个数据流(data-stre ...

  8. FunDA(17)- 示范:异常处理与事后处理 - Exceptions handling and Finalizers

    作为一个能安全运行的工具库,为了保证占用资源的安全性,对异常处理(exception handling)和事后处理(final clean-up)的支持是不可或缺的.FunDA的数据流FDAPipeL ...

  9. FunDA(15)- 示范:任务并行运算 - user task parallel execution

    FunDA的并行运算施用就是对用户自定义函数的并行运算.原理上就是把一个输入流截分成多个输入流并行地输入到一个自定义函数的多个运行实例.这些函数运行实例同时在各自不同的线程里同步运算直至耗尽所有输入. ...

随机推荐

  1. source Insight工程的简单使用

    本文以管理虚拟机里面的uboot为例: 1.选择project->New project->选择工程路径,假设为D:\uboot:->project has its own conf ...

  2. python之BeautifulSoup模块

    # 名称修改(bs4) from bs4 import BeautifulSoup 帮助文档 Beautiful Soup parses a (possibly invalid) XML or HTM ...

  3. python数据类型4

    一浮点数 什么叫做浮点数:浮点数就相当于小数,但是浮点数不包括无限循环又不重复的小数. 小数分为 有限小数和无限小数 无限小数又分为 无限循环小数和无限不循环小数 而 浮点数就是有限小数和无限循环小数 ...

  4. 2018.10.20 bzoj1068: [SCOI2007]压缩(区间dp)

    传送门 这题转移很妙啊. f[l][r][1/0]f[l][r][1/0]f[l][r][1/0]表示对于区间[l,r][l,r][l,r]有/无重复的机会时压缩的最小值. 那么可以从三种情况转移过来 ...

  5. 2018.10.20 loj#2593. 「NOIP2010」乌龟棋(多维dp)

    传送门 f[i][j][k][l]f[i][j][k][l]f[i][j][k][l]表示用iii张111,jjj张222,kkk张333,lll张444能凑出的最大贡献. 然后从f[i−1][j][ ...

  6. 2018.08.09洛谷P3959 宝藏(随机化贪心)

    传送门 回想起了自己赛场上乱搜的20分. 好吧现在也就是写了一个随机化贪心就水过去了,不得不说随机化贪心大法好. 代码: #include<bits/stdc++.h> using nam ...

  7. afx_msg解释

    以前一直不知道AFX_MSG是什么意思,只是觉得它应该是个消息映射函数,但是具体代表什么意思,会返回一个什么样的值是一点都不清楚,今天查了下资料,把查到的东西放这,以免以后忘了还得再查. 在头文件(D ...

  8. HDU1241 Oil Deposits 2016-07-24 13:38 66人阅读 评论(0) 收藏

    Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detectin ...

  9. 对Spring 容器管理事务支持的总结

    1.问题 Connection conn = DataSourceUtils.getConnection(); //开启事务 conn.setAutoCommit(false); try { Obje ...

  10. Launch Google Map in Android / IOS Mobile

    <!--This only works in android mobile phone--><a href="geo:0,0?q=myaddress+encode)__&q ...