Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm having a little problem with Scala Actors and blocking IO. I'm using an actor that itself has an anonymous actor that consumes an input stream. The problem is that this stream only reads one line and then blocks without waking up again. What confuses me is that it worked when the consumption took place in the act method (coincidence?). The application itself has some more actors and java threads doing some work.
My question is: What is the common practice to avoid such problems? Any pointers?
The code that causes this problem looks somewhat like this:
object TestActor extends Actor {
private val instream = actor {
loop {
try {
println(processInput(input.readLine)) //bufferedinputstream. blocks, no wakeup
} catch {
case e: Exception =>
println(e.toString)
}
}
}
def act(): Unit = {
react {
...
case _ => {}
}
}
}
Regards,
raichoo
The call to readLine is inside loop{} so it's going to happen over and over until it blocks.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
This is some of the strange issue, the program is only printing hello scala, but it seems that I am getting syntax weird error. Please correct me if I miss anything
Program
object hello{
def main(args:Array[String]){
println("Hello Scala")
}
}
Error:- '=' expected, but '{' found
As the error message suggests, you need a = before the {:
def main(args: Array[String]) = {
println("Hello Scala")
}
The syntax you used was originally supported so you might see it in some example code. But it was deprecated in later versions and is now no longer allowed.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I built an object which is supposed to serialize and deserialize a file. I built it in scala in terminal on mac. I'm new to scala and don't have that much programming experience in general. I was wondering how I could run the object so that it performs the task I assigned it to do. Might be a dumb question but I don't know what to do. Here's an image of what my object is
Enter in terminal (repl)
serializationTest3.main(Array())
"Running" an object is actually executing the method main(Array[String]): Unit of the object.
Extending trait App
object X extends App {
foo()
}
is like wrapping the object body with method main
object X {
def main(args: Array[String]): Unit = {
foo()
}
}
https://www.scala-lang.org/api/2.13.3/scala/App.html
How Scala App trait and main works internally?
Difference between object with main() and extends App in scala
Difference between using App trait and main method in scala
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to write a TCP server which handles multiple connections.
I know the way Java does it with sockets is that it has a pool of threads, the server has a while loop waiting for connections and when 'accpets' one it 'gets' a thread from the pool to handle the request.
I was picturing something similar with actors.
class TcpServer(remote: InetSocketAddress) extends Actor {
import Tcp._
import context.system
IO(Tcp) ! Bind(self, remote)
def receive = {
case b # Bound(localAddress) =>
context.parent ! b
case CommandFailed(_: Bind) ⇒ context stop self
case c # Connected(remote, local) =>
val handler: ActorRef = context.actorOf(RoundRobinPool(5).props(Props[SimplisticHandler]), "router")
sender() ! Register(handler)
}
But obviously, that is not enough. What am i missing here?
For Akka TCP the pattern generally used is to have one actor handle the connection. Since actors get scheduled onto threads as needed by the ActorSystem's dispatcher, the pattern of getting a thread from a thread pool is provided more or less automatically by Akka (it's possible to, for instance, configure the default dispatcher to be a pool of a single thread, but that's not default and not recommended).
With that in mind, you'll want to replace the
context.actorOf(RoundRobinPool(5).props(Props[SimplisticHandler]), "router")
with
context.actorOf(Props[SimplisticHandler])
and be sure in SimplisticHandler to
context.stop.self
in response to a ConnectionClosed message.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I understand the concept of None
I also understand how nice it is to have MyObj and also MyEmptyObj but i cant figure out when its better to use None instead of MyEmptyObj and when I should be using MyEmptyObj instead of None. Also I tend not to like to see methods that return Option[MyObj] it clutters my code I prefer to return MyObj and then seamlessly call its methods such as MyObj.toJson and thus MyEmptyObj.toJson will know to represent itself rather than having case None: return some empty json what do you think about this whole subject?
To the other side I can say that None goes very well with flatMap etc, so which to choose?
when to None
and when to Empty?
None is great when you have no default value, but if you have a default, by all means use it.
As a very simple example, if you wanted to define a function called amountOwed, you could do this:
def amountOwed(bill: Int, alreadyPaid: Option[Int]): Option[Int] = {
val owed = bill - alreadyPaid.getOrElse(0)
if(owed == 0) None // nothing to pay!
else Some(owed)
}
But that's much more complex (and annoying) than it needs to be, since 0 makes perfect sense as both a "haven't paid anything" and "don't owe anything":
def amountOwed(bill: Int, alreadyPaid: Int): Int = {
bill - alreadyPaid
}
I want to do a multi-agent simulation containing about 10.000 agents (machine and product agents) using the Scala Actor framework.
As I understand, if there are lots of actors passing messages around, can it run out of stack due the recursion?
If so, how can I increase the stack sizes for the underlying worker threads?
The actor framework has been designed to handle this - in fact, it can handle this with only one thread, assuming you use the loop-react pattern as follows:
import actors._
import actors.Actor._
val a = actor {
loop {
react {
case ABC => //Handle here
}
}
}
On pp 590-593 of Programming in Scala this is discussed in more detail: basically the react method never returns normally (it terminates with an exception) and therefore its call stack does not need to be preserved. You can think of it as looping forever.