-
-
Notifications
You must be signed in to change notification settings - Fork 244
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
Add Task.forkAndForget #520
Comments
I would love to attempt this with other implementation, I assume it has some benefits and it would be something similar to |
Sure, go for it. Just FYI — it should do an automatic fork, so in the implementation you should use |
@alexandru My naive implementation: def apply[A](fa: Task[A]): Task[Unit] =
Task.Async[Unit] { (ctx, cb) =>
// Light async boundary to avoid stack overflows
ctx.scheduler.execute(new TrampolinedRunnable {
def run(): Unit = {
implicit val sc = ctx.scheduler
// Standard Scala promise gets used for storing or waiting
// for the final result
val p = Promise[Unit]()
// Building the Task to signal, linked to the above Promise.
// It needs its own context, its own cancelable
val ctx2 = Task.Context(ctx.scheduler, ctx.options)
TaskFromFuture.build(p.future, ctx2.connection)
// Starting actual execution of our newly created task
Task.unsafeStartAsync(fa.map(_ => ()), ctx2, Callback.fromPromise(p))
// Signal the created Task reference
cb.onSuccess(())
}
})
} Seems to do the trick (it passes the law) but I'm not confident that it is entirely correct. I don't fully understand it and have handful of questions related to it: Why is How does Is I will really appreciate any explanation! I wandered into |
You don't need a
Because we need to do memoization. We're creating a And we already have But all this is relevant for
Every
The callback is signaling that processing is finished, where processing represents just the start of the child task. In the case the parent is done processing since you've started the child, with the actual execution being represented by the signaled child in NOTE — this is the current implementation of start, but because you're using |
@alexandru Thanks that makes sense and clear some things for me, hopefully I'm pretty close in my implementation in #530. |
For
Task
we now have.start
and.fork
with the same semantics, except thatfork
also forces a logical thread fork:Now we need something like this:
Following law should apply, but the implementation doesn't need to be this:
N.B. this law probably can't be tested since
forkAndForget
is all about triggering side effects.The text was updated successfully, but these errors were encountered: