Sasha Chepurnoi
06/29/2020, 2:24 PMSasha Chepurnoi
06/29/2020, 2:26 PMfun foo() = IO.fx{
val fiberOne = IO{}.fork(dispatchers().io()).bind()
val fiberTwo = IO{}.fork(dispatchers().io()).bind()
val res1 = fiberOne.join().bind()
val res2 = fiberTwo.join().bind()
res1+res2
}simon.vergauwen
06/29/2020, 2:40 PMres1 + res2 doesn’t run on IO pool, and you’d want it to run there?Sasha Chepurnoi
06/29/2020, 2:41 PMSasha Chepurnoi
06/29/2020, 2:42 PMSasha Chepurnoi
06/29/2020, 2:42 PMsimon.vergauwen
06/29/2020, 2:46 PMfork launches the IO you call it on.
It does so inside IORunLoop given a certain CoroutineContext.
The result is memoized, and that is done using an internal UnsafePromise. Once it completes, the UnsafePromise will cache it.
Awaiting the UnsafePromise is no different than awaiting a callback with IO.async.
That brings us to the threading, join happens on the caller thread.
So if IO.fx is running on a worker pool (IO.dispatchers().default()), and your two fibers are running on dispatchers().io(). Then the two fibers will run on IO Pool, and the joined results will be on worker pool.simon.vergauwen
06/29/2020, 2:47 PMSasha Chepurnoi
06/29/2020, 3:05 PMThread: Test worker
Thread: Test worker
Thread: Test worker
Thread: io-arrow-kt-worker-0
Thread: io-arrow-kt-worker-0
So always when I join the fiber, the IO will continue on fiber’s thread?simon.vergauwen
06/30/2020, 4:22 PMIO API in suspend style. Which also works in the way you expect it here to work. Sorry about that.simon.vergauwen
06/30/2020, 4:23 PMcontinueOn to switch to any desired context in an IO.fx block.simon.vergauwen
06/30/2020, 4:24 PMIO. This’ll happen in over the summer 🙂Sasha Chepurnoi
06/30/2020, 4:52 PM