Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

An asynchronous programming facility for Scala

License

NotificationsYou must be signed in to change notification settings

scala/scala-async

Repository files navigation

A Scala DSL to enable a direct style of coding when composingFutures.

Usage

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"%"1.0.1"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 suffixto 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.1</version></dependency><dependency>  <groupId>org.scala-lang</groupId>  <artifactId>scala-reflect</artifactId>  <version>2.13.8</version>  <scope>provided</scope></dependency>

Enable compiler support forasync

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

importscala.concurrent.ExecutionContext.Implicits.globalimportscala.async.Async.{async,await}valfuture= async {valf1:Future[Boolean]= async { ...;true }valf2= async { ...;42 }if (await(f1)) await(f2)else0}

What isasync?

async marks a block of asynchronous code. Such a block usually containsone or moreawait calls, which marks a point at which the computationwill be suspended until the awaitedFuture is complete.

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

Consider the following example:

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

Line 1 defines an asynchronous method: it returns aFuture.

Line 2 begins anasync block. During compilation,the contents of this block will be analyzed to identifytheawait calls, and transformed into non-blockingcode.

Control flow will immediately pass to line 5, as thecomputation in theasync block is not executedon the caller's thread.

Line 3 begins by triggeringslowCalcFuture, and thensuspending until it has been calculated. Only after ithas finished, we trigger it again, and suspend again.Finally, we add the results and completecombined, whichin 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:

defcombined:Future[Int]= async {valfuture1= slowCalcFuturevalfuture2= slowCalcFuture  await(future1)+ await(future2)}

Limitations

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

Theawait 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 withintry /catch /finally.

This implementation restriction may be lifted in future versions.

Comparison with direct use ofFuture API

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

defslowCalcFuture:Future[Int]= ...valfuture1= slowCalcFuturevalfuture2= slowCalcFuturedefcombined:Future[Int]=for {  r1<- future1  r2<- future2}yield r1+ r2

Theasync approach has two advantages over the use ofmap andflatMap:

  1. The code more directly reflects the programmer's intent,and does not require us to name the resultsr1 andr2.This advantage is even more pronounced when we mix controlstructures inasync blocks.
  2. async blocks are compiled to a single anonymous class,as opposed to a separate anonymous class for each closurerequired at each generator (<-) in the for-comprehension.This reduces the size of generated code, and can avoid boxingof intermediate results.

[8]ページ先頭

©2009-2025 Movatter.jp