Parallel IO Example #1439
-
I'm looking through the v5 language-ext Samples folder for an example of parallel processing but I can't find any references to What I'm trying to get at is something equivalent to
Thanks for your time! Glendon |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To make any operation run in parallel you can use You can use it like so: var delayed = IO.liftAsync(async () =>
{
await Task.Delay(3000);
return unit;
});
var readKey = IO.lift(Console.ReadKey);
var startTimer = IO.lift(Stopwatch.StartNew);
var writeLine = (object? value) => IO.lift(() => Console.WriteLine(value));
var op = from watch in startTimer
from fork1 in IO.fork(delayed)
from fork2 in IO.fork(delayed)
from fork3 in IO.fork(delayed)
from _1 in fork1.Await >> fork2.Await >> fork3.Await
from _2 in writeLine($"Elapsed: {watch.ElapsedMillisecon}")
select unit;
op.Run().Ignore(); I realise that there's no useful
Another way that it can work is: awaitAll(IO.fork(delayed), IO.fork(delayed), IO.fork(delayed)) Note, all of the above is for parallel processing. If you just want concurrent processing, then use I notice you're using // Take any function
Func<X> f = ...;
// Make it into an IO operation (synchronous)
IO<X> synchronousIO = IO.lift(f);
// Fork it to launch on another thread
IO<ForkIO<X>> parallelIO = IO.fork(synchronousIO); If the function already returns a |
Beta Was this translation helpful? Give feedback.
To make any operation run in parallel you can use
IO.fork
, it works withIO<A>
or anyK<M, A>
where theM
trait-implementation supportsMonadIO.ToIO
.You can use it like so: