gpt4 book ai didi

performance - Scala Actor : long running io operations

转载 作者:行者123 更新时间:2023-12-04 15:05:12 24 4
gpt4 key购买 nike

对于包含长时间运行的操作的actor,我遇到了一些麻烦,在我的例子中是持久的套接字连接。下面是一些测试代码,如果我创建的 Server 实例少于四个,它们运行良好,但是如果我创建更多的实例,我总是最终只有三个或有时四个并发套接字连接,因为其他连接超时。
我想知道为什么会这样,以及我的代码是否有明显的错误。

package test

import actors.Actor
import actors.Actor._
import java.io.{PrintStream, DataOutputStream, DataInputStream}
import java.net.{Socket, InetAddress}
import java.text.{SimpleDateFormat}
import java.util.{Calendar}

case class SInput(input: String)
case class SOutput(output: String)
case class SClose
case class SRepeat

import scala.xml._

class Config(xml: Node) {
var nick: String = (xml \ "nick").text
var realName: String = (xml \ "realName").text
var server: String = (xml \ "ip").text
var port: Int = (xml \ "port").text.toInt
var identPass: String = (xml \ "identPass").text
var joinChannels: List[String] = List.fromString((xml \ "join").text.trim, ' ')
}

object ServerStarter {
def main(args: Array[String]): Unit = {
var servers = List[Server]()

val a = actor {
loop {
receive {
case config: Config =>
actor {
val server = new Server(config)
servers = server :: servers
server.start
}
}
}
}

val xml = XML.loadFile("config.xml")
(xml \ "server").elements.foreach(config => a ! new Config(config))
}
}


class Server(config: Config) extends Actor {
private var auth = false
private val socket = new Socket(InetAddress.getByName(config.server), config.port)
private val out = new PrintStream(new DataOutputStream(socket.getOutputStream()))
private val in = new DataInputStream(socket.getInputStream())

def act = {
val _self = this
_self ! SRepeat

while (true) {
receive {
case SRepeat =>
try {
val input = in.readLine
if (input != null) {
actor {_self ! SInput(input)}
} else {
actor {_self ! SClose}
}
} catch {
case e: Exception =>
println(e)
actor {_self ! SClose}
}

case SClose =>
println(getDate + " closing: " + config.server + " mail: " + mailboxSize)
try {
socket.close
in.close
out.close
} catch {
case e: Exception =>
println(e)
}

case SInput(input: String) =>
println(getDate + " " + config.server + " IN => " + input + " mail: " + mailboxSize)
actor {onServerInput(_self, input)}
_self ! SRepeat

case SOutput(output: String) =>
println(getDate + " " + config.server + " OUT => " + output + " mail: " + mailboxSize)
actor {
out.println(output)
out.flush()
}

case x =>
println("unmatched: " + x + " mail: " + mailboxSize)
}
}
}

private def getDate = {
new SimpleDateFormat("hh:mm:ss").format(Calendar.getInstance().getTime());
}

def onServerInput(a: Actor, input: String) = {
if (!auth) {
authenticate(a)
}
else if (input.contains("MOTD")) {
identify(a)
join(a)
}
else if (input.contains("PING")) {
pong(a, input)
} else {
}
}

def authenticate(a: Actor) = {
a ! SOutput("NICK " + config.nick)
a ! SOutput("USER " + config.nick + " 0 0 : " + config.realName)
auth = true
}

def pong(a: Actor, input: String) = {
a ! SOutput("PONG " + input.split(":").last)
}

def identify(a: Actor) = {
if (config.identPass != "") {
a ! SOutput("nickserv :identify " + config.nick + " " + config.identPass)
}
}

def join(a: Actor) = {
config.joinChannels.foreach(channel => a ! SOutput("JOIN " + channel))
}
}

顺便提一句。我使用的是 Scala 2.7.6 final。

最佳答案

这里有奇怪的事情。例如:

actor { 
val server = new Server(config)
servers = server :: servers
server.start
}

或者也:
actor {_self ! SClose}  

方法 actor是一家 Actor 工厂。例如,在第一种情况下,您正在创建一个角色,它将创建另一个角色(因为服务器是一个角色),然后启动它。

让我重复一遍: actor {之间的所有内容和 }是 Actor 。在那个 Actor 里面,你在做什么 new Server ,这会创建另一个 Actor 。那是在 receive 里面,当然,这是 Actor 的一部分。因此,在 Actor 内部,您正在创建将创建 Actor 的 Actor 。

在第二个示例中,您正在创建一个 Actor ,只是为了向自己发送消息。这对我来说毫无意义,但我对 Actor 的经验并不多。

关于performance - Scala Actor : long running io operations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2064806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com