Question

I am tring a simple akka data-flow sample, just like following:

import akka.dataflow._ //to get the flow method and implicit conversions

object FlowTestApp extends App {
    import scala.concurrent.ExecutionContext.Implicits.global

    flow { 
      println("in flow")
      "Hello world!" 
    } onComplete println
}

I also set continuations:enable in the P filed of the scala compiler setting. but after run the program, I just got nothing in the console, no error, no "hello, world" either. What did I miss?

Was it helpful?

Solution

import akka.dataflow._
import scala.concurrent._, ExecutionContext.Implicits._

// the following are for duration
import scala.language.postfixOps
import scala.concurrent.duration._

object FlowTestApp extends App {

  val f = flow {
    println("in flow")
    "Hello world!"
  } onComplete println

  Await.ready(f, 1 second)
}

If you're thinking about using Akka Dataflow as a non-blocking alternative to threads, here's my 2 cents:

Some core language features (e.g. loops, try/finally) break in the delimited code blocks. I've been using it for some I/O and I'm happy with it, but I'd make sure to keep it under the hood.

%99 of akka-dataflow is Scala's continuations plugin. The definition of flow() (which boggles my mind) and the pimped Future#apply() are really all that's needed, and they're quite small. Scala's continuations plugin, however, is much more generic than the problem of non-blocking threads.

For an alternative solution, keep track of scala-async, which has a much more specific approach (transforming your seemingly sequential code into future { ... } flatMap { ... } blocks)

I tried it last week, and as of that time scala-async's code transformation still covered less than what is supported by akka-dataflow, but from looking at the commit log it seems to be gaining fast and will soon surpass it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top