logo
Tags down

shadow

Scala Futures Returning Empty List after Await


By : Shannon Hilliard
Date : July 31 2020, 11:00 AM
hop of those help? Mixing mutable collections and concurrency is not a good idea. Consider refactoring checkEntry to return, say, Future[Option[Entry]] instead of Future[Boolean], where Some would represent successful validation, whilst None unssucessful, and then you might do something like
code :
case class Entry(v: Int)
val queuedEntries = List(Entry(1), Entry(2), Entry(3))
def checkEntry(entry: Entry): Future[Option[Entry]] = ???

Future
  .traverse(queuedEntries)(checkEntry)
  .map(_.flatten)
case class Entry(v: Int)
val queuedEntries = List(Entry(1), Entry(2), Entry(3))
def checkEntry(entry: Entry): Future[Boolean] = Future(Random.nextBoolean)

Future
  .traverse(queuedEntries)(checkEntry)
  .map(checkedEntries => checkedEntries zip queuedEntries)
  .map(_.collect { case (validated, entry) if validated => entry} )


Share : facebook icon twitter icon

Returning two Scala futures when both end


By : wilv83
Date : March 29 2020, 07:55 AM
Any of those help You combine Future's in a map, so it looks like you have to return a Seq of such Future's:
code :
def method1 ( s: Seq[Int]): Seq[Future [(Int,Int)]] =
  s.map { sx =>
    val future = someFutureMethod1 (sx)
    val future2 = someFutureMethod2 (sx)
    future.zip(future2)
  }

Scala - How to use a Timer without blocking on Futures with Await.result


By : Carlos Mendoza
Date : March 29 2020, 07:55 AM
wish of those help One solution would be to use Akka's after function which will let you pass a duration, after which the future throws an exception or whatever you want.
Take a look here. It demonstrates how to implement this.
code :
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
import akka.actor.ActorSystem
import akka.pattern.after

val system = ActorSystem("theSystem")

lazy val f = future { Thread.sleep(2000); true }
lazy val t = after(duration = 1 second, using = system.scheduler)(Future.failed(new TimeoutException("Future timed out!")))

val fWithTimeout = Future firstCompletedOf Seq(f, t)

fWithTimeout.onComplete {
   case Success(x) => println(x)
   case Failure(error) => println(error)
}

How to avoid Await.ready in Scala while using Futures?


By : Mr Spambo
Date : March 29 2020, 07:55 AM
Does that help I have following pseudo code. Invoke fetch, fetchRecordDetail, upload and notifyUploaded functions in sequence. Each function returns a future event but the first functions returns Option[T], going forward(fetchRecordDetail, upload and notifyUploaded) calls I need to carry only Some[T] type and ignore None. , This appears to work.
code :
Future.sequence(ids.map(fetch))                             //fetch Recs
  .map(_.flatten)                                           //remove None
  .flatMap(rs=> Future.sequence(rs.map(fetchRecordDetail))) //fetch Details
  .flatMap(ds=> Future.sequence(ds.map(upload)))            //upload
  .flatMap(notifyUploaded)                                  //notify

Scala Futures: Returning a future with empty map leaves the Future uncompleted


By : Dima_Brijatov
Date : March 29 2020, 07:55 AM
I wish this help you When I tried mapping on future response I get in scala REPL 2.11.7 is,
code :
scala> import scala.concurrent.Future
import scala.concurrent.Future

scala> import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext

scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

scala> getFutureMap(0).map { print}
Map()res7: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@3f92a84e

scala> getFutureMap(100).map { print}
res8: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@574cd322
Map(69 -> 69, 88 -> 88, 5 -> 5, 10 -> 10, 56 -> 56, 42 -> 42, 24 -> 24, 37 -> 37, 25 -> 25, 52 -> 52, 14 -> 14, 20 -> 20, 46 -> 46, 93 -> 93, 57 -> 57, 78 -> 78, 29 -> 29, 84 -> 84, 61 -> 61, 89 -> 89, 1 -> 1, 74 -> 74, 6 -> 6, 60 -> 60, 85 -> 85, 28 -> 28, 38 -> 38, 70 -> 70, 21 -> 21, 33 -> 33, 92 -> 92, 65 -> 65, 97 -> 97, 9 -> 9, 53 -> 53, 77 -> 77, 96 -> 96, 13 -> 13, 41 -> 41, 73 -> 73, 2 -> 2, 32 -> 32, 34 -> 34, 45 -> 45, 64 -> 64, 17 -> 17, 22 -> 22, 44 -> 44, 59 -> 59, 27 -> 27, 71 -> 71, 12 -> 12, 54 -> 54, 49 -> 49, 86 -> 86, 81 -> 81, 76 -> 76, 7 -> 7, 39 -> 39, 98 -> 98, 91 -> 91, 66 -> 66, 3 -> 3, 80 -> 80, 35 -> 35, 48 -> 48, 63 -> 63, 18 -> 18, 95 -> 95, 50 -> 50, 67 -> 67, 16 -> 16, 31 -> 31, 11 -> 11, 72 -> 72, 43 -> 43, 99 -> 99, 87 -> 87, 40 -> 40, 26 -> 26, 55 -> 55, 23 -> 23, 8 -> 8, 75 -> 75, 58 -> 58, 82 -> 82, 36 -> 36, 30 -> 30, 51 -> 51, 19 -> 19, 4 -> 4, 79 -> 79, 94 -> 94, 47 -> 47, 15 -> 15, 68 -> 68, 62 -> 62, 90 -> 90, 83 -> 83, 100 -> 100)
scala> import scala.util.Success
import scala.util.Success

scala> import scala.util.Failure
import scala.util.Failure

scala> getFutureMap(0).onComplete {
     | case Success(x) => println(x)
     |  case Failure(y) => println(y)
     | }
Map()
scala>  getFutureMap(5).onComplete {
     |    case Success(x) => println(x)
     |    case Failure(y) => println(y)
     |  }
Map(5 -> 5, 1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4)

Difference between Await.result and futures.onComplete in Scala


By : Topher
Date : March 29 2020, 07:55 AM
I wish did fix the issue. onComplete runs on some arbitrary (unspecified) thread in the ExecutionContext, whereas Await.result runs on the current thread, and blocks it until it completes or the specified timeout is exceeded. The first is non-blocking, the second is blocking.
There's also a difference in how failures are handled in the two snippets, but this is kind of obvious from looking at the code.
shadow
Privacy Policy - Terms - Contact Us © voile276.org