New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task.runSyncUnsafe #542

Closed
alexandru opened this Issue Jan 18, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@alexandru
Member

alexandru commented Jan 18, 2018

Monix has long held the philosophy that disallowing blocking operations is healthy, deferring users to Await.result on the generated Future values.

However for Task a blocking operation makes some sense because:

  1. we can do better than Await.result on a Future value — for example the first step of the run-loop can be executed in a synchronous model (e.g. ExecutionModel.SynchronousExecution) until the first real async boundary is hit, because introducing batched async boundaries to encourage fairness doesn't make sense when you block the current thread
  2. for benchmarks it would have slightly better performance because Await.result on a Future has some overhead (to be proven by benchmark)
  3. Task is meant to encourage pure FP and even though Task is a nice citizen in mixed projects (given its Future integration), it's meant to be used at "the end of the world" (e.g. in TaskApp) where blocking isn't an issue
  4. we can disallow this operation on top of JavaScript with a nice error message
sealed abstract class Task[+A] {

  def runSyncUnsafe(timeout: Duration = Duration.Inf)
    (implicit s: Scheduler, permit: CanBlock): A

  def runSyncUnsafeOpt(timeout: Duration = Duration.Inf)
    (implicit s: Scheduler, opts: Options, permit: CanBlock): A
}

On top of JavaScript this CanBlock can provide a very nice error message:

@implicitNotFound("Blocking operations are not allowed on top of JavaScript engines")
final class CanBlock private ()

@alexandru alexandru added this to the 3.0.0 milestone Jan 18, 2018

@alexandru alexandru self-assigned this Jan 18, 2018

@alexandru alexandru changed the title from Task.runSync to Task.runBlocking Jan 18, 2018

@alexandru alexandru changed the title from Task.runBlocking to Task.runSyncUnsafe Jan 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment