Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用play.api.libs.ws批处理请求_Scala_Playframework 2.0 - Fatal编程技术网

Scala 使用play.api.libs.ws批处理请求

Scala 使用play.api.libs.ws批处理请求,scala,playframework-2.0,Scala,Playframework 2.0,我有一个脚本,它可以发出很多web请求(~300000)。看起来像这样 // Setup a new wsClient val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build val builder = new AsyncHttpClientConfig.Builder(config) val wsClient = new NingWSClient(builder.build) // Ea

我有一个脚本,它可以发出很多web请求(~300000)。看起来像这样

// Setup a new wsClient
val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build
val builder = new AsyncHttpClientConfig.Builder(config)
val wsClient = new NingWSClient(builder.build)

// Each of these use the wsClient
def getAs: Future[Seq[A]] = { ... }
def getBs: Future[Seq[B]] = { ... }
def getCs: Future[Seq[C]] = { ... }
def getDs: Future[Seq[D]] = { ... }

(for {
    as <- getAs
    bs <- getBs
    cs <- getCs
    ds <- getDs
} yield (as, bs, cs, ds)).map(tuple => println("done"))
但这会导致理解过程提前结束(没有任何错误消息或异常):

(用于{

正如我遇到了一个类似的问题,对一个web服务的请求太多(~500+)。 您的分组代码示例几乎是正确的,但是,如果您使用
Iterator[Future[List[Int]]]
或者
Future.sequence
-d it
Future[Iterator[List[Int]]]
,您将获得
Iterator[Future[List[Int]]]]
。但是,我认为它们都将异步运行。您需要启动第一批,然后启动
flatMap
(等待完成),然后启动下一批。这是我设法编写的,如下所示:

希望这有帮助!

您可以使用Octoparts:

但听起来你真的想扭转这种模式,让wsClient在外部进行调用,然后你就可以平面映射未来[WSResponse]这将限制AsyncHttpClient使用的内部Netty线程池的未来数,您可以更改配置设置以增加或减少Netty通道池中的线程数

def getAs: Future[Seq[A]] = {
    someCollection.group(1000).map(batch => {
        val client = new NingWSClient(builder.build) // Make a new client for every batch
        Future.sequence(batch.map(thing => {
            wsClient.url(...).map(...)
        })).map(things => {
            wsClient.close // Close the client
            things
        })
    })
}
(for {
    as <- getAs
    bs <- getBs // This doesn't happen
    cs <- getCs // Or any of the following ones
    ds <- getDs
} yield (as, bs, cs, ds)).map(tuple => println("done"))
val futureIterator = list.grouped(50).foldLeft(Future.successful[List[Int]](Nil)) {
  (fItems, items) =>
    fItems flatMap { processed =>
      println("PROCESSED: " + processed); println("SPAWNED: " + items);
      Future.traverse(items)(getFuture) map (res => processed ::: res)
    }
}
println(Await.result(futureIterator, Duration.Inf))