The react / whenever designed to process one message at a time and in full. The fact is that the state held in the react block is safe because of this.
There may be several workers reading from Channel ; they just need to be configured as follows:
my @sums; for ^4 { push @sums, start react whenever $KXGA -> $number { say "In thread ", $*THREAD.id; say "β ", (^$number).sum; } }
This react approach, when used with use v6.d.PREVIEW , has the advantage that, with less work, it will not actually occupy 4 threads, and they can continue to work with other pools. If instead the entire application simply processes the material from Channel , and thatβs all, you will have less overhead and better locality with just:
my @sums; for ^4 { push @sums, start for $KXGA.list -> $number { say "In thread ", $*THREAD.id; say "β ", (^$number).sum; } }
Unlike react there is no way to react to different data sources in this approach (the reason Channel works with react is primarily that you can consume them and work with asynchronous data sources at the same time, or possibly deal with multiple channels at the same time) . But if you donβt need it, then the for method is a bit simpler in the code and almost certainly faster (and, as in the case of react , the loops compete over the elements in the Channel and end gracefully when it is closed).
source share