scala - Is blocking within a future still blocking? -
blocking bad, async good, blocking within future still blocking? keep coming to; consider following pseudo-code:
def queryname(id:id):future[string] def queryeveryonesnames:future[seq[string]] = { val everyonesids:future[seq[id]] = getids val everyonesnames:future[seq[future[string]]] = { everyonesids.map(seq.map(id=>queryname(id))) } // i'm trying understand impact of i'll below everyonesnames.map(seq=>seq.map(fut=>blocking(fut, 1 s))) } queryeveryonesnames
in last line turned future[seq[future[string]]]
(notice future within future) future[seq[string]]
blocking on inner future.
blocking on future within future feels redundant, @ least here, yet having future within future feels redundant well.
can propose smarter way of getting rid of inner future?
do think blocking on future inside future bad? if why , under circumstances?
yes, future blocking blocking, should avoid that, resources blocked wait result, if in thread.
if understood correctly, question how convert future[seq[future[string]]]
future[seq[string]]
in non-blocking way.
you can for-comprehensions:
val in = future[seq[future[string]]] val m = for( <- in ) // seq[future[string]] yield ( future.sequence(a)) // yields m = future[future[seq[string]]] val result = for(a <- m; b <- a) yield (b) // yields future[seq[string]]
edit: or just:
val result = in.flatmap(a => future.sequence(a))
Comments
Post a Comment