hyper/race and Promises
I am experimenting with executing some subroutine for all the elements in a list. The first thing I tried was hyper/race
for race (^6) {
sleep 1;
say $_;
}
however it executes sequentially. For me the solution was to use Promises as
await (^6).map: {
start {
sleep 1;
say $_;
}
}
My questions are:
- Why
hyper/raceis not working as I intend? - Why we have both methods if they are almost equivalent?
Posted Sep 10, 2019, 7:26 PM UTC
The default values for the two configurable aspects of hyper/race won't work for your example:
:batchis how many iterations of an iterable operation to batch together at a time. The default is64. You need it to be1.:degreeis how many batches to try running in parallel. The default is4. You probably want it to be6.To specify these you must apply the
.hyperor.racemethod to anIterable. But tohyper/raceaforyou must also specify thehyper/racestatement prefix before thefor. Putting this all together:hyper/raceare for parallelism.await/Promises are for asynchrony. For more info see my favorite answer to the quora question "What are the differences between parallel, concurrent, and asynchronous programming?"Thanks for the explanation. I think my second question was unclear: I meant that
hyper/racemight not be needed if they can be implemented in terms of a sequence of promises, but from your explanation I understand that the batch size would be implicitly1, and that I might not be able to set the degree either (I suppose it would be set implicitly as well to my actual number of cores).