scala/scala-async là một trong những repo mã nguồn mở mà TopGit theo dõi, hiện có 1.2k sao, viết chủ yếu bằng Scala. An asynchronous programming facility for Scala
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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.
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:
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:
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.
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.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/scala/scala-async là nguồn chính thức.
scala/scala-async có phải mã nguồn mở không?
Có — scala/scala-async phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/scala/scala-async.
scala/scala-async là gì?
scala/scala-async (scala/scala-async) là dự án Scala trên GitHub. Theo mô tả gốc: An asynchronous programming facility for Scala
Đọc đầy đủ README ở tab phía trên.
scala-async có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về scala-async.