Closed NeQuissimus closed 6 years ago
Maybe in this very case it is not the reason, but in general i'd not recommend to try concurrent-ish stuff on the REPL, I see you are using Ammonite, dunno if it is the same as with sbt console
but I always get better (expected) behavior when writing a little app in a scala file and using sbt run
io1
is forked, so will complete almost instantaneously, not in 10 seconds.
io2
runs forever, so will never complete.Thus io2
will be terminated very quickly, after some non-deterministic number of println
.
What I think you want is simple X.race(Y)
, not X.race(Y.fork)
.
Oh! I thought I had to fork at least one of them. This makes sense...
@NeQuissimus Let the types be your guide! 😉 If they allow to do something, then it's a valid operation (or if it's not, then I have some work to do).
@jdegoes .toUnit
should have probably been enough of a hint that I need to rethink what I am doing :D
OK, so here I thought race was straight-forward. Clearly I am doing something wrong...
I was under the impression the two
IO
would race each other and once the first one finishes (clearly the sleeper, since the other one is aforever
guy), the slower one is interrupted and will never be heard of again. In the above codeio2
is completely unfazed by the race and will keep on running forever. Can anIO.forever
not be interrupted?Anyways, I changed things around a bit, thinking maybe one of the fibres is not allowed to be forked, and now I have this:
a) The race lasts maybe 1 second, not 10, not forever b) Sometimes I get a bunch of prints but usually only one
Here is essentially what I need (maybe there is a significantly better way to do this):
IO[Error, Unit]
(let's call themX
andY
)X
runs forever (it's a streaming processor)Y
does a bunch of stuff, then completesX
to start, thenY
to start. OnceY
has completed, interruptX
, collect results, done