Skip to content

Instantly share code, notes, and snippets.

@13h3r2

13h3r2/catsEffectConcurrent.scala Secret

Last active Sep 23, 2018
Embed
What would you like to do?
Context shift
import java.util.concurrent.Executors
import cats.effect._
import cats.effect.concurrent.Deferred
import cats.effect.implicits._
import cats.effect.internals.IOContextShift
import cats.implicits._
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
object TTT extends App {
def test[F[_]]()(implicit F: ConcurrentEffect[F], T: Timer[F], C: ContextShift[F]): F[Unit] = {
val computation: F[Unit] = F.delay {
println("Computation started")
//this should block thread. it represents cpu intensive computation
Thread.sleep(100)
println("Computation done")
}
for {
d <- Deferred[F, Unit]
// it works sequentially without `C.shift` here
tasks = List.fill(10)(computation).map(c => d.get *> C.shift *> c)
fibers <- tasks.foldLeft(F.pure(List.empty[Fiber[F, Unit]])) {
(acc, t) => acc.flatMap(list => t.start.map(_ :: list))
}
resultFiber <- fibers.foldLeft(F.unit)(_ *> _.join).start
_ <- T.sleep(100 millis)
_ <- d.complete(())
result <- resultFiber.join
} yield result
}
implicit val _cs: ContextShift[IO] = IOContextShift(ExecutionContext.fromExecutor(Executors.newFixedThreadPool(20)))
implicit val _t: Timer[IO] = IO.timer(ExecutionContext.Implicits.global)
test[IO]().unsafeRunSync()
}
package shield
import cats.effect._
import cats.effect.concurrent.Deferred
import cats.effect.implicits._
import cats.implicits._
import monix.eval.Task
import monix.execution.Scheduler
import scala.concurrent.duration._
object TTT extends App {
def test[F[_]]()(implicit F: ConcurrentEffect[F], T: Timer[F]): F[Unit] = {
val computation: F[Unit] = F.delay {
println("Computation started")
//this should block thread. it represents cpu intensive computation
Thread.sleep(100)
println("Computation done")
}
for {
d <- Deferred[F, Unit]
// it works sequentially without `C.shift` here
tasks = List.fill(10)(computation).map(c => d.get /**> C.shift */*> c)
fibers <- tasks.foldLeft(F.pure(List.empty[Fiber[F, Unit]])) {
(acc, t) => acc.flatMap(list => t.start.map(_ :: list))
}
resultFiber <- fibers.foldLeft(F.unit)(_ *> _.join).start
_ <- T.sleep(100 millis)
_ <- d.complete(())
result <- resultFiber.join
} yield result
}
implicit val _sch = Scheduler.Implicits.global
import monix.execution.schedulers.CanBlock._
test[Task]().runSyncUnsafe(10 seconds)
}
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
java.lang.ClassCastException: monix.eval.Task$Eval cannot be cast to cats.effect.IO
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1(TaskEffect.scala:59)
at monix.eval.internal.TaskEffect$.$anonfun$cancelable$1$adapted(TaskEffect.scala:52)
at monix.eval.internal.TaskEffect$$$Lambda$56/112797691.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:110)
at monix.eval.Task$.unsafeStartNow(Task.scala:3047)
at monix.eval.internal.TaskStart$$anon$1.run(TaskStart.scala:48)
at monix.execution.schedulers.TrampolineExecutionContext.monix$execution$schedulers$TrampolineExecutionContext$$localRunLoop(TrampolineExecutionContext.scala:109)
at monix.execution.schedulers.TrampolineExecutionContext.startLoopOptimal(TrampolineExecutionContext.scala:93)
at monix.execution.schedulers.TrampolineExecutionContext.execute(TrampolineExecutionContext.scala:78)
at monix.execution.schedulers.BatchingScheduler.execute(BatchingScheduler.scala:50)
at monix.execution.schedulers.BatchingScheduler.execute$(BatchingScheduler.scala:47)
at monix.execution.schedulers.AsyncScheduler.execute(AsyncScheduler.scala:29)
at monix.eval.internal.TaskStart$.$anonfun$apply$1(TaskStart.scala:35)
at monix.eval.internal.TaskStart$.$anonfun$apply$1$adapted(TaskStart.scala:33)
at monix.eval.internal.TaskStart$$$Lambda$51/1781071780.apply(Unknown Source)
at monix.eval.internal.TaskRunLoop$.executeAsyncTask(TaskRunLoop.scala:421)
at monix.eval.internal.TaskRunSyncUnsafe$.blockForResult(TaskRunSyncUnsafe.scala:135)
at monix.eval.internal.TaskRunSyncUnsafe$.apply(TaskRunSyncUnsafe.scala:94)
at monix.eval.Task.runSyncUnsafe(Task.scala:571)
at shield.TTT$.delayedEndpoint$shield$TTT$1(TTT.scala:47)
at shield.TTT$delayedInit$body.apply(TTT.scala:16)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.App$$Lambda$5/1395089624.apply(Unknown Source)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at shield.TTT$.main(TTT.scala:16)
at shield.TTT.main(TTT.scala)
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Computation started
Computation done
Process finished with exit code 0
package shield
import cats.effect._
import cats.effect.concurrent.Deferred
import cats.effect.implicits._
import cats.implicits._
import monix.eval.Task
import monix.execution.Scheduler
import scala.concurrent.duration._
object TTT extends App {
def test[F[_]]()(implicit F: ConcurrentEffect[F], T: Timer[F]): F[Unit] = {
val computation: F[Unit] = {
val item = List.fill(100) {
//this represents a small chunk of cpu bound task
F.delay(Thread.sleep(1))
}
F.delay(println("Computation started")) *>
item.foldM(())((a, b) => b) *>
F.delay(println("Computation done"))
}
for {
d <- Deferred[F, Unit]
// it works sequentially without `C.shift` here
tasks = List.fill(10)(computation).map(c => d.get /**> C.shift */*> c)
fibers <- tasks.traverse(_.start)
resultFiber <- fibers.foldM(())((_, f) => f.join).start
_ <- T.sleep(100 millis)
_ <- d.complete(())
result <- resultFiber.join
} yield result
}
implicit val _sch = Scheduler.Implicits.global
import monix.execution.schedulers.CanBlock._
test[Task]().runSyncUnsafe(10 seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment