6

Both C# and Scala have adopted frameworks for simplifying doing asynchronous/parallel computation, but in different ways. The latest C# (5.0, still in beta) has decided on an async/await framework (using continuation-passing under the hood, but in an easier-to-use way), while Scala instead uses the concept of "actors", and has recently taken the actors implementation in Akka and incorporated it into the base library.

Here's a task to consider: We receive a series of requests to do various operations -- e.g. from user input, requests to a server, etc. Some operations are fast, but some take awhile. For the slow ones, we'd like to asynchronously do the operation (in another thread) and process it when the thread is done, while still being free to process new requests.

A simple synchronous loop might be (pseudo-code):

while (1) {
  val request = waitForAnything(user_request, server_request)
  val result = do_request(request)
  if (result needs to be sent back)
    send_back_result(result)
}

In a basic fork/join framework, you might do something like this (pseudo-code):

val threads: Set[Thread]

while (1) {
  val request = waitForAnything(user_request, server_request, termination of thread)
  if (request is thread_terminate) {
    threads.delete(request.terminated_thread)
    val result = request.thread_result
    if (result needs to be sent back)
      send_back_result(result)
  } else if (request is slow) {
    val thread = new Thread(() => do_request(request))
    Threads.add(thread)
    thread.start()
  } else {
    val result = do_request(request)
    if (result needs to be sent back)
      send_back_result(result)
  }
}

How would this look expressed using async/await and using actors, and more generally what are the advantages/disadvantages of these approach?

Urban Vagabond
  • 7,282
  • 3
  • 28
  • 31
  • Can you please be more precise on your question? Examples are easy to find using Google, so what is your question? How these approaches compare? – Lucero Jul 22 '12 at 09:40
  • 1
    I don't know Akka, but .Net 4.5 also contains an actors-based library: TPL Dataflow. – svick Jul 22 '12 at 10:07
  • 9
    async/await is completely unrelated to the Actor Model (Actors). Async/await has more in common with Akka Futures & Akka Dataflow. – Viktor Klang Jul 22 '12 at 12:23
  • 2
    C#'s `await` is like Scala's `react`, but without having to pass a continuation. Scala has the `loop` rewriting, but C#'s `await` can rewrite any method. In general, I think C#'s `await`, `Task`, and TPL Dataflow are more lower-level than Scala's actors - but also more flexible. Disclaimer: I learned everything I know about Scala in 5 minutes [from this](http://www.scala-lang.org/node/242). – Stephen Cleary Jul 22 '12 at 13:10
  • As Victor already said, scala's actors are not synonim to C# async, instead scala uses concept called Futures (not only Akka ones, but in [scala itself](http://docs.scala-lang.org/sips/pending/futures-promises.html), in [Twitter library](https://github.com/twitter/util/blob/master/util-core/src/main/scala/com/twitter/util/Future.scala), in [Dispatch library](http://databinder.net/dispatch-doc/#dispatch.futures.Futures) and some other implementations). I highly recommend you to read the first link (SIP) to get the feeling what is the Future. – om-nom-nom Jul 22 '12 at 15:08
  • OK, I would say that a Future in Scala is roughly equivalent to an awaitable in C# (the `Task` class is one awaitable); a Promise is equivalent to `TaskCompletionSource`; and Future callbacks and `map` are equivalent to task continuations. Scala does have some higher-level combinations on Futures (`loop`, `recover`, `foreach`, `andThen`, projections, etc) which is a different approach than `async`. `async` instructs the compiler to rewrite the method (breaking it into continuations automatically) instead of taking a library-based approach which still requires explicit continuations. – Stephen Cleary Jul 22 '12 at 17:58

3 Answers3

5

Please consider mine as a partial answer: "old" Scala actors have been replaced by Akka actors , which are much more then a simple async/await library.

  1. Akka actors are "message-handlers" which are organized into a hierarchy which can be running on one or more JVMs and even distributed across a network.
  2. When you realize your asynchronous processing requires actors (read later why this is not forcely necessary), Akka let you and helps you to put in place the best patterns in terms of failure handling, dispatching and routing
  3. Akka comes with different transport layers, and other fancies ready-to-use facilities such as explicit Finite State Machines, Dataflow concurrency and others.
  4. Akka comes with Futures, which are more likely to corresponds to the Async/Await framework in C# 5.0

You can read more about Akka futures on the Akka website or on this post:

Parallel file processing in Scala

Community
  • 1
  • 1
Edmondo
  • 19,559
  • 13
  • 62
  • 115
2

I can't speak for Scala, but the C# version would look something like this (I don't have an IDE handy so pardon any errors/typos:

public async Task<int> GetResult()
{
   while (true)
   {
      var request = await waitForAnything(user_request, server_request);
      var result = await do_request(request);
      if (isValid(result))
         return result;
   }
}

And you would call it something like so:

public void RunTheProgram()
{
   int result = await GetResult();
   Console.WriteLine("Result: {0}", result);
}

In short, the actual code would look very much like the pseudo code, i.e. very much like how a normal human being would think about the problem. That's the real beauty of C#'s async/await.

gzak
  • 3,908
  • 6
  • 33
  • 56
  • 1
    you can only use await in a async method. Since `RunTheProgram` is not asynchronous, you should use `int result = GetResult().Result`; – Fabio Marreco Oct 15 '14 at 21:01
2

Scala has implemented also the async/await paradigm, which can simplify some algorithms.

Here is the proposal: http://docs.scala-lang.org/sips/pending/async.html

Here is the implementation: https://github.com/scala/async

Sanete
  • 621
  • 1
  • 6
  • 4