All Projects → scala → Scala Async

scala / Scala Async

Licence: apache-2.0
An asynchronous programming facility for Scala

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Scala Async

drone-stm32-map
STM32 peripheral mappings for Drone, an Embedded Operating System.
Stars: ✭ 16 (-98.51%)
Mutual labels:  asynchronous, concurrency
Polyel-Framework
⚡️ Voltis Core: A PHP framework based on Swoole from the ground up
Stars: ✭ 22 (-97.96%)
Mutual labels:  asynchronous, concurrency
Hunch
Hunch provides functions like: All, First, Retry, Waterfall etc., that makes asynchronous flow control more intuitive.
Stars: ✭ 94 (-91.27%)
Mutual labels:  asynchronous, concurrency
Bastion
Highly-available Distributed Fault-tolerant Runtime
Stars: ✭ 2,333 (+116.62%)
Mutual labels:  asynchronous, concurrency
Zio
ZIO — A type-safe, composable library for async and concurrent programming in Scala
Stars: ✭ 3,167 (+194.06%)
Mutual labels:  asynchronous, concurrency
Threadly
A library of tools to assist with safe concurrent java development. Providing unique priority based thread pools, and ways to distrbute threaded work safely.
Stars: ✭ 196 (-81.8%)
Mutual labels:  asynchronous, concurrency
ComposableAsync
Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
Stars: ✭ 28 (-97.4%)
Mutual labels:  asynchronous, concurrency
Vibe Core
Repository for the next generation of vibe.d's core package.
Stars: ✭ 56 (-94.8%)
Mutual labels:  asynchronous, concurrency
drone-cortexm
ARM® Cortex®-M platform crate for Drone, an Embedded Operating System.
Stars: ✭ 31 (-97.12%)
Mutual labels:  asynchronous, concurrency
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (-97.12%)
Mutual labels:  asynchronous, concurrency
Lear
Linux Engine for Asset Retrieval - speed-profiled C HTTP server
Stars: ✭ 165 (-84.68%)
Mutual labels:  asynchronous, concurrency
Rxgo
Reactive Extensions for the Go language.
Stars: ✭ 3,907 (+262.77%)
Mutual labels:  asynchronous, concurrency
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (-86.63%)
Mutual labels:  asynchronous, concurrency
java-red
Effective Concurrency Modules for Java
Stars: ✭ 25 (-97.68%)
Mutual labels:  asynchronous, concurrency
Drone
CLI utility for Drone, an Embedded Operating System.
Stars: ✭ 114 (-89.42%)
Mutual labels:  asynchronous, concurrency
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (-90.34%)
Mutual labels:  asynchronous, concurrency
Ea Async
EA Async implements async-await methods in the JVM.
Stars: ✭ 1,085 (+0.74%)
Mutual labels:  asynchronous, concurrency
hunt
A refined core library for D programming language. The module has concurrency / collections / event / io / logging / text / serialization and more.
Stars: ✭ 86 (-92.01%)
Mutual labels:  asynchronous, concurrency
Drone Core
The core crate for Drone, an Embedded Operating System.
Stars: ✭ 263 (-75.58%)
Mutual labels:  asynchronous, concurrency
Jdonframework
Domain-Driven-Design Pub/Sub Domain-Events framework
Stars: ✭ 978 (-9.19%)
Mutual labels:  asynchronous, concurrency

scala-async Build Status

A DSL to enable a direct style of programming with when composing values wrapped in Scala Futures.

Quick start

To include scala-async in an existing project use the library published on Maven Central. For sbt projects add the following to your build definition - build.sbt or project/Build.scala:

Use a modern Scala compiler

As of scala-async 1.0, Scala 2.12.12+ or 2.13.3+ are required.

Add dependency

SBT Example

libraryDependencies += "org.scala-lang.modules" %% "scala-async" % "0.10.0"
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided

For Maven projects add the following to your (make sure to use the correct Scala version suffix to match your project’s Scala binary version):

Maven Example

<dependency>
  <groupId>org.scala-lang.modules</groupId>
  <artifactId>scala-async_2.13</artifactId>
  <version>1.0.0</version>
</dependency>
<dependency>
  <groupId>org.scala-lang</groupId>
  <artifactId>scala-reflect</artifactId>
  <version>2.13.5</version>
  <scope>provided</scope>
</dependency>

Enable compiler support for async

Add the -Xasync to the Scala compiler options.

SBT Example

scalacOptions += "-Xasync"

Maven Example

<project>
  ...
  <plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>4.4.0</version>
    <configuration>
      <args>
        <arg>-Xasync</arg>
      </args>
    </configuration>
  </plugin>
  ...
</project>

Start coding

import scala.concurrent.ExecutionContext.Implicits.global
import scala.async.Async.{async, await}

val future = async {
  val f1: Future[Boolean] = async { ...; true }
  val f2 = async { ...; 42 }
  if (await(f1)) await(f2) else 0
}

What is async?

async marks a block of asynchronous code. Such a block usually contains one or more await calls, which marks a point at which the computation will be suspended until the awaited Future is complete.

By default, async blocks operate on scala.concurrent.{Future, Promise}. The system can be adapted to alternative implementations of the Future pattern.

Consider the following example:

def slowCalcFuture: Future[Int] = ...             // 01
def combined: Future[Int] = async {               // 02
  await(slowCalcFuture) + await(slowCalcFuture)   // 03
}
val x: Int = Await.result(combined, 10.seconds)   // 05

Line 1 defines an asynchronous method: it returns a Future.

Line 2 begins an async block. During compilation, the contents of this block will be analyzed to identify the await calls, and transformed into non-blocking code.

Control flow will immediately pass to line 5, as the computation in the async block is not executed on the caller's thread.

Line 3 begins by triggering slowCalcFuture, and then suspending until it has been calculated. Only after it has finished, we trigger it again, and suspend again. Finally, we add the results and complete combined, which in turn will release line 5 (unless it had already timed out).

It is important to note that while lines 1-4 are non-blocking, they are not parallel. If we wanted to parallelize the two computations, we could rearrange the code as follows:

def combined: Future[Int] = async {
  val future1 = slowCalcFuture
  val future2 = slowCalcFuture
  await(future1) + await(future2)
}

Limitations

await must be directly in the control flow of the async expression

The await cannot be nested under a local method, object, class or lambda:

async {
  List(1).foreach { x => await(f(x) } // invalid
}

await must be not be nested within try / catch / finally.

This implementation restriction may be lifted in future versions.

Comparison with direct use of Future API

This computation could also be expressed by directly using the higher-order functions of Futures:

def slowCalcFuture: Future[Int] = ...
val future1 = slowCalcFuture
val future2 = slowCalcFuture
def combined: Future[Int] = for {
  r1 <- future1
  r2 <- future2
} yield r1 + r2

The async approach has two advantages over the use of map and flatMap:

  1. The code more directly reflects the programmer's intent, and does not require us to name the results r1 and r2. This advantage is even more pronounced when we mix control structures in async blocks.
  2. async blocks are compiled to a single anonymous class, as opposed to a separate anonymous class for each closure required at each generator (<-) in the for-comprehension. This reduces the size of generated code, and can avoid boxing of intermediate results.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].