tags:

views:

730

answers:

3
+5  Q: 

Sleeping actors?

What's the best way to have an actor sleep? I have actors set up as agents which want to maintain different parts of a database (including getting data from external sources). For a number of reasons (including not overloading the database or communications and general load issues), I want the actors to sleep between each operation. I'm looking at something like 10 actor objects.

The actors will run pretty much infinitely, as there will always be new data coming in, or sitting in a table waiting to be propagated to other parts of the database etc. The idea is for the database to be as complete as possible at any point in time.

I could do this with an infinite loop, and a sleep at the end of each loop, but according to http://www.scala-lang.org/node/242 actors use a thread pool which is expanded whenever all threads are blocked. So I imagine a Thread.sleep in each actor would be a bad idea as would waste threads unnecessarily.

I could perhaps have a central actor with its own loop that sends messages to subscribers on a clock (like async event clock observers)?

Has anyone done anything similar or have any suggestions? Sorry for extra (perhaps superfluous) information.

Cheers

Joe

+8  A: 

There's no need to explicitly cause an actor to sleep: using loop and react for each actor means that the underlying thread pool will have waiting threads whilst there are no messages for the actors to process.

In the case that you want to schedule events for your actors to process, this is pretty easy using a single-threaded scheduler from the java.util.concurrent utilities:

object Scheduler {
    private lazy val sched = Executors.newSingleThreadedScheduledExecutor();
    def schedule(f: => Unit, long time) {
        sched.schedule(new Runnable {
          def run = {
            actors.Scheduler.execute(f) 
          }
        }, time - Platform.currentTime, TimeUnit.MILLISECONDS);
    }
}

You could extend this to take periodic tasks and it might be used thus:

val execTime = //...  
Scheduler.schedule( { Actor.actor { target ! message }; () }, execTime)

Your target actor will then simply need to implement an appropriate react loop to process the given message. There is no need for you to have any actor sleep.

oxbow_lakes
+4  A: 

There was a good point to Erlang in the first answer, but it seems disappeared. You can do the same Erlang-like trick with Scala actors easily. E.g. let's create a scheduler that does not use threads:

import scala.actors.{Actor,Exit,TIMEOUT}

object Scheduler {
  def schedule(f: => Unit, time: Long) = new AnyRef {
    import Actor._
    private val a = actor { loop }
    private def loop: Unit = reactWithin(time) {
      case TIMEOUT => f; loop
      case Exit => 
    }

    def stop() { a ! Exit }
  }
}

And let's test it (I did it right in Scala REPL) using a test client actor:

case class Ping(t: Long)

import Actor._
val test = actor { loop {
  receiveWithin(3000) {
    case Ping(t) => println(t/1000)
    case TIMEOUT => println("TIMEOUT")
    case Exit => exit
  }
} }

Run the scheduler:

val scheduler = Scheduler.schedule({ test ! Ping(System.currentTimeMillis) }, 2000)

and you will see something like this

scala> 1249383399
1249383401
1249383403
1249383405
1249383407

which means the scheduler sends message every 2 seconds as expected. Let's stop the scheduler:

scheduler.stop()

the test client will begin to report timeouts:

scala> TIMEOUT
TIMEOUT
TIMEOUT

stop it as well:

test ! Exit
Alexander Azarov
+1  A: 

ActorPing (Apache License) from lift-util has schedule and scheduleAtFixedRate Source: ActorPing.scala

From scaladoc:

The ActorPing object schedules an actor to be ping-ed with a given message at specific intervals. The schedule methods return a ScheduledFuture object which can be cancelled if necessary