- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Tôi đang sử dụng scala
hiện hữu play framework
中调用一个 web 服务
。代码遵循 Producer/Consumer
模式。每次调用 WS 大约需要 2 秒。但是很多这样的调用都超过了 120 秒(这是 Play 中的默认超时)。因此它抛出一个异常:java.net.connectException
正好在 120 秒后。
câu hỏi:
为什么将所有调用的时间加起来,而不是单独处理它们,因此超时不会成为问题。
我通过解决这个问题尝试了一种增加超时的解决方案:fixed ws.timeout .但对我来说,问题仍然存在。
是线程还是并发的问题?
这是类的代码:
class WS(sentenceList: List[String], queue: BlockingQueue[Future[Response]], filename: String) {
val listofJson = new ListBuffer[(String, JsValue)]
listofJson.clear
def callWSProducer() = {
sentenceList.foreach { name =>
val data = Json.obj(
"input_sent" -> name,
"Filename" -> filename)
val holder: Future[Response] = WS.url("http://0.0.0.0:8015/endpoint/").withHeaders("Content-Type" -> "application/json").post(data)
implicit val context = scala.concurrent.ExecutionContext.Implicits.global
queue.put(holder)
}
}
def WSConsumer(): List[(String, JsValue)] = {
sentenceList.foreach { name =>
val result = Await.result(queue.take(), 100.second)
val out = (result.json \ "sentence");
listofJson += ((name, out));
}
return listofJson.toList
}
}
我进入控制台时出错:
biên tập:
让我把问题说得更清楚一点。首先,通过创建上述类的对象从 Controller (主线程)调用上述函数。上面的 Json 列表返回给 Controller , Controller 又将其返回给 View 。因为我们必须返回列表,所以我们能想到的唯一可能的方法是使用等待(阻塞)机制。
我知道代码存在线程问题,但至少有人可以指出这些问题。我们尝试过的所有方法都会导致上面提到的 120 秒超时,或者当我们的等待 block 中存在某种死锁时导致 100 秒 future 超时,例如当我们使用类似于此处提到的解决方案时:Scala Play Resolve a list of futures
1 Câu trả lời
我很困惑为什么我们必须阻止?我想我们可以为此想出一个非阻塞的解决方案。但首先让我们解决超时问题。对于 WS,您可以使用 ws.timeout 属性配置超时。我不确定为什么那不起作用。对于在您的 application.conf 文件中设置为 0 的实验
ws.timeout=0
这实际上是将超时设置为永远。这可能会在您的代码中引发新问题。我不确定。现在让我们看看阻塞/等待的东西。这样的事情怎么样
object Application extends Controller {
//using Scala global implicits is a bad practice
//implicit val context = scala.concurrent.ExecutionContext.Implicits.global
//use the play one or even better use a custom execution context read
//http://www.playframework.com/documentation/2.1.0/ThreadPools
implicit val context = play.api.libs.concurrent.Execution.Implicits.defaultContext
def index = Action.async {
invokeServices(send your sentence list).map { listOfPairs =>
//do your transformation here to create the result
}
}
//invokeServices hopefully capture all that you are doing inside the WS class but no blocking
//and mutation.
def invokeServices(sentenceList: List[String]): Future[List[(String, JsValue)]] = {
val responses: List[Future[(String, JsValue)]] = sentenceList.map { name =>
val data = Json.obj(
"input_sent" -> name,
"Filename" -> name)
val response: Future[Response] =
WS.url("http://0.0.0.0:8015/endpoint/").withHeaders("Content-Type" -> "application/json").post(data)
response.map(result => (name, result.json \ "sentence"))
}
//converts List[Future[A]] to Future[List[A]]
Future.sequence(responses)
}
}
关于java - 如何解决play framework 2.2.1 connect timeout异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22174845/
我正在使用 heroku 来托管支持 iOS 应用程序的 ruby on rails 应用程序。我有一个可能会运行很长时间的请求,我需要能够在我的请求被终止之前捕获超时。我正在使用 Timeout
我在 https://stackoverflow.com/questions/517219?tab=oldest#tab-top 找到了我认为应该完美运行的东西但是,它对我不起作用。 我在 Windo
我有这段代码: begin complete_results = Timeout.timeout(4) do results = platform.search(artist,
我正在开发一个音乐训练游戏,我使用 Unity 3D 来创建它。 它会发出随机音符。 问题是,我的 android 设备只有五个“屏幕熄灭前的时间”选项,其中最长的是 10 分钟。 所以 10 分钟后
我运行以下代码来捕获任何可能挂起的 SQL 语句。在尝试对此进行测试时,我编写了一个非常优化的 sql 语句,它需要一分钟的时间才能运行。我在 activerecord execute sql 语句周
由于 Faraday 没有文档,我无法在任何地方找到它。法拉第什么是“timeout”,什么是“open timeout”? 最佳答案 如果您在 https://github.com/lostisla
我想对 Angular.js $timeout 进行单元测试,以检查是否已使用正确的持续时间/延迟值调用它。 断言看起来像这样: expect($timeout).toHaveBeenCalledWi
我正在循环一个列表并对列表中的每个成员执行一些操作。如果某个成员花费了太多时间(在本例中为 1 秒),我打算跳过它。但是,try 语句内的 block 始终处于处理状态,并且永远不会超时。我不明白为什
我有一个程序可以打印出通过或失败。我想检测卡在那里的程序并回显“超时” 我写了这样一个脚本: #!/bin/bash echo -n 'test' && timeout 5 ./mytest | gr
我有一个 Sinatra 应用程序。我正在使用 Rack::Test 对其进行测试。我想确保将查询字符串参数传递给 Timeout::timeout()。 我认为 expect_any_instanc
相同的脚本不同的错误。这可能更多地与我的网络有关,而不是我的代码。脚本如下: #!/usr/bin/env ruby -rubygems require File.join(File.dirname(
我需要测试一个从 url 加载图像的 AngularJs 服务。这是我的服务: /*global angular, Image*/ (function () { 'use strict'; f
随着数据库大小的增加,我有一个查询需要更长的时间来执行。查询已优化并且是必要的,但我的 C# 控制台应用程序最近给我这个错误: Unhandled Exception: MySql.Data.MySq
我正在研究 Linux shell 中的 timeout 命令。 当我尝试 timeout 1 bash 时,bash 将运行并在 1 秒后终止。 当我尝试 timeout 2 timeout 1 y
随着数据库大小的增加,我有一个查询需要更长的时间来执行。查询已优化并且是必要的,但我的 C# 控制台应用程序最近给我这个错误: Unhandled Exception: MySql.Data.MySq
我希望使用 Spring boot 和 Tomcat 扩展 Spring MVC 应用程序中的用户 session 。查看文档似乎有 2 个相关 properties : server.servlet
我正在尝试升级 Puppet 以使用 Ruby 1.9,但遇到了常量问题。 const_defined?("Timeout") 返回真,即使 :Timeout 不在常量列表中。这不会发生在 Ruby
首先,这是一个几乎重复的: How to differentiate when wait(long timeout) exit for notify or timeout? 但这是一个新的后续问题。
对于下面的代码,notifyAll() 会一直持有锁直到完成,即使超时了,这个 block 也不持有锁,必须等待notifyAll() block 完成。那么wait(timeout)中的timeou
Thread.Sleep(timeout) 和resetEvent.Wait(timeout) 都会导致执行暂停至少timeout 毫秒,那么它们之间有区别吗?我知道 Thread.Sleep 导致线
Tôi là một lập trình viên xuất sắc, rất giỏi!