C # 5.0 async / await examples against Akka acres vs basic fork / join?

Both C # and Scala adopted a framework to simplify asynchronous / parallel computing, but in different ways. The latest version of C # (5.0, still in beta) decided to use the async / wait structure (using a continuation that goes under the hood, but is easier to use), while Scala instead uses the concept of “actors” and more recently accepted actors in Akka and included them in the base library.

Here's the problem: We get a series of requests to perform various operations - for example, from user input, server requests, etc. Some operations are quick, but some take some time. For slow ones, we would like to perform an operation asynchronously (in another thread) and process it when the thread is running, but at the same time are free to process new requests.

A simple synchronous loop can 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 the base fork / join structure, you can do something like this (pseudocode):

 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) } } 

What would it look like using asynchronous / waiting and using participants, and in general, what are the advantages / disadvantages of this approach?

+6
source share
3 answers

Please consider mine as a partial answer: the "old" Scala actors have been replaced by Akka actors, which are much more than a simple asynchronous / waiting library.

  • Akkova actors are “message handlers” that are organized into a hierarchy that can run on one or more JVMs and is even distributed across the network.
  • When you understand that your asynchronous processing requires participants (read later why this is not necessary), Akka allows you to create better templates in terms of failover, dispatch, and routing.
  • Akka comes with various transport layers, while others come with ready-to-use tools, such as explicit end machines, Dataflow concurrency, and others.
  • Akka ships with futures that are more likely to match the Async / Await structure in C # 5.0

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

Parallel file processing in Scala

+5
source

I can not speak for Scala, but the C # version will look something like this (I do not have an IDE, so I apologize for 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 this:

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

In short, the actual code will be very similar to pseudocode, i.e. very similar to how a normal person would think about a problem. This is the true beauty of C # async / await.

+2
source

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

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

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

+2
source

Source: https://habr.com/ru/post/921063/


All Articles