无法将 IO 转移到不同的线程池
Not able to shift the IO on a different thread pool
我正在阅读关于猫效果的教程
https://typelevel.org/blog/2017/05/02/io-monad-for-cats.html
我根据这个教程写了这段代码
import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors
import cats.effect.IO
val Main = ExecutionContext.global
val BlockingIO = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
val program = for {
_ <- IO { println("what is your name") }
name <- IO { readLine() }.shift(BlockingIO).shift(Main)
} yield s"Hello $name"
val output = program.unsafeRunSync
println(output)
我收到错误 value shift is not a member of cats.effect.IO[String]
本教程如何移动 readLines
函数的结果
lines <- readLines("names.txt").shift(BlockingFileIO).shift(Main)
我对这个示例的最终全局是 readLine
的块发生在我的 BlockingIO 池中。
好的。我自己找到了答案。我认为该教程有点过时
https://typelevel.org/cats-effect/datatypes/io.html
val program = for {
_ <- IO { println("what is your name") }
_ <- IO.shift(BlockingIO)
name <- IO { readLine }
_ <- IO.shift(Main)
} yield s"Hello $name"
我正在阅读关于猫效果的教程
https://typelevel.org/blog/2017/05/02/io-monad-for-cats.html
我根据这个教程写了这段代码
import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors
import cats.effect.IO
val Main = ExecutionContext.global
val BlockingIO = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
val program = for {
_ <- IO { println("what is your name") }
name <- IO { readLine() }.shift(BlockingIO).shift(Main)
} yield s"Hello $name"
val output = program.unsafeRunSync
println(output)
我收到错误 value shift is not a member of cats.effect.IO[String]
本教程如何移动 readLines
函数的结果
lines <- readLines("names.txt").shift(BlockingFileIO).shift(Main)
我对这个示例的最终全局是 readLine
的块发生在我的 BlockingIO 池中。
好的。我自己找到了答案。我认为该教程有点过时
https://typelevel.org/cats-effect/datatypes/io.html
val program = for {
_ <- IO { println("what is your name") }
_ <- IO.shift(BlockingIO)
name <- IO { readLine }
_ <- IO.shift(Main)
} yield s"Hello $name"