ParallelFlux vs flatMap () for blocking I / O task

I have a Project Reactor chain that includes a blocking task (network call, we need to wait for a response). I would like to run multiple blocking tasks at the same time.

It seems that either ParallelFlux or flatMap () can be used, bare-bone examples:

Flux.just(1)
    .repeat(10)
    .parallel(3)
    .runOn(Schedulers.elastic())
    .doOnNext(i -> blockingTask())
    .sequential()
    .subscribe()

      

or

Flux.just(1)
    .repeat(10)
    .flatMap(i -> Mono.fromCallable(() -> {blockingTask(); return i;}).subscribeOn(Schedulers.elastic()), 3)
    .subscribe();

      

What are the merits of the two methods? Can you be preferable to the other? Are there any alternatives?

+3


source to share


1 answer


parallel

is designed to parallelize tasks for performance purposes and dispatch work between "rails" or "groups", each of which gets its own execution context from Scheduler

which you pass to runOn

. In short, it will put all of your CPU cores to work if you are CPU intensive. But you are doing the I / O bound work ...

So, in your case flatMap

, the best candidate. This use flatMap

for parallelization has more to do with orchestration.



These are pretty much 2 alternatives, unless you consider a slightly different flavor flatMap

to that flatMapSequential

( concatMap

doesn't actually allow parallelization).

+6


source







All Articles