Allows elements of a stream to be broadcast to multiple workers.
Allows elements of a stream to be broadcast to multiple workers.
As the elements arrive, they are broadcast to all workers
that have started evaluation before the
element was pulled.
Elements are pulled as chunks from the source and the next chunk is pulled when all workers are done
with processing the current chunk. This behaviour may slow down processing of incoming chunks by
faster workers. If this is not desired, consider using the prefetch
and prefetchN
combinators on workers to compensate
for slower workers.
Often this combinator is used together with parJoin, such as :
Stream(1,2,3,4).broadcast.map { worker => worker.evalMap { o => IO.println("1:" + o.toString) } }.take(3).parJoinUnbounded.compile.drain.unsafeRunSync
Note that in the example, above the workers are not guaranteed to see all elements emitted. This is
due to different subscription times of each worker and speed of emitting the elements by the source.
If this is not desired, consider using broacastThrough
and broadcastTo
, which are built on top of Broadcast.through
, as an alternative.
They will hold on pulling from source if there are no workers ready.
When source
terminates, then the inner streams (workers) are terminated once all elements pulled
from source
are processed by all workers. However, note that when that source
terminates,
resulting stream will not terminate until the inner streams terminate.
specifies that broadcasting will hold off until at least minReady
subscribers will
be ready
Like apply but instead of providing a stream of worker streams, it runs each inner stream through the supplied pipes.
Like apply but instead of providing a stream of worker streams, it runs each inner stream through the supplied pipes.
Supplied pipes are run concurrently with each other. Hence, the number of pipes determines concurrency.
Also, this guarantees that each sink will view all O
pulled from source stream, unlike broadcast
.
Resulting values are collected and returned in a single stream of O2
values.
pipes that will concurrently process the work
Provides mechanisms for broadcast distribution of elements to multiple streams.