project reactor - ParallelFlux vs flatMap() for a Blocking I/O task -
i have project reactor chain includes blocking task (a network call, need wait response). i'd run multiple blocking tasks concurrently.
it seems either parallelflux or flatmap() 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 merits of 2 techniques? 1 preferred on other? there alternatives?
parallel
tailored parallelization of tasks performance purposes, , dispatching of work between "rails" or "groups", each of own execution context scheduler
pass runon
. in short, put cpu cores work if cpu intensive work. you're doing i/o bound work...
so in case, flatmap
better candidate. use of flatmap
parallelization more orchestration.
these pretty 2 alternatives, if don't count different flavor of flatmap
flatmapsequential
(concatmap
doesn't allow parallelization).
Comments
Post a Comment