Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:41:01+00:00 2026-05-28T06:41:01+00:00

I am spawning a small number of actors to fetch, process and save RSS

  • 0

I am spawning a small number of actors to fetch, process and save RSS feed items to a database. This is done through a main method of an object running on cron. I create these actors and dole out jobs to them as they complete the previous job assigned to them. My main class spawns a single actor, the one that doles out jobs to a pool of actors. Eventually the main method seems to hang. It doesn’t exit, but execution halts on all the actors. My CTO believes the main is exiting before the actors complete their work and leaving them, but I am not convinced that’s the case. I receive no success exit on main (no exit at all).

Essentially I’m wondering how to debug these actors, and what possible reason could cause this to happen. Will main exit before actors have completed their execution (and if it does, does that matter?) From what I can tell actors using receive are mapped 1-to-1 to threads, correct? Code is below. Please ask any follow-up questions, help is greatly appreciated. I know I may not have provided sufficient detail, I’m new to scala and actors and will update as needed.

object ActorTester {
  val poolSize = 10
  var pendingQueue :Set[RssFeed] = RssFeed.pendingQueue

  def main(args :Array[String]) {
    val manager = new SpinnerManager(poolSize, pendingQueue)
    manager.start
  }
}

case object Stop

class SpinnerManager(poolSize :Int = 1, var pendingQueue :Set[RssFeed]) extends Actor {
  val pool = new Array[Spinner](poolSize)

  override def start() :Actor = {
    for (i <- 0 to (poolSize - 1)) {
      val spinner = new Spinner(i)
      spinner.start()
      pool(i) = spinner
    }
    super.start
  }

  def act() {
    for {
      s <- pool
      if (!pendingQueue.isEmpty)
     } {
       s ! pendingQueue.head
       pendingQueue = pendingQueue.tail
     }

    while(true) {
      receive {
        case id :Int => {
          if (!pendingQueue.isEmpty) {
            pool(id) ! pendingQueue.head
            pendingQueue = pendingQueue.tail             
          } else if ((true /: pool) { (done, s) => {
            if (s.getState != Actor.State.Runnable) {
              val exited = future {
                s ! Stop
                done && true
              }
              exited()
            } else {
              done && false
            }
          }}) {
            exit
          }
        } 
      }
    }
  }
}

class Spinner(id :Int) extends Actor {
  def act() {
    while(true) {
      receive {
        case dbFeed :RssFeed => {
          //process rss feed
          //this has multiple network requests, to the original blogs, bing image api
          //our instance of solr - some of these spawn their own actors
          sender ! id
        }
        case Stop => exit
      }
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:41:02+00:00Added an answer on May 28, 2026 at 6:41 am

    So after several days of debugging I’ve solved this issue. fotNelton’s code suggestions were very helpful in doing so, so I’ve given him a vote. However, they didn’t address the problem itself. What I’ve found is that if you are running this in a main method then if the parent actors exit before their child actors then the program will hang forever and never exit, still holding all of its memory. In the process of handling the RSS feed, a Fetcher would spawn actors and send them messages to do things involving network requests. These actors need to complete their work before the parent actor quits. Fetcher wouldn’t wait for these actors to finish though, once he sent the message he would just move on. So he would tell manager he was finished before his child actors had finished all their work. To deal with this, one option would be to use futures and wait until the actors are done (pretty slow). My solution was to create services accessible via URL (POST to a service that has an actor waiting to react). The service would respond right away, and send a message to its own actor. Thus the actors can quit once they send the request to the service, and don’t need to spawn any other actors.

    object FeedFetcher {
      val poolSize = 10
      var pendingQueue :Queue[RssFeed] = RssFeed.pendingQueue
    
      def main(args :Array[String]) {
        new FetcherManager(poolSize, pendingQueue).start
      }
    }
    
    case object Stop
    
    class FetcherManager(poolSize :Int = 1, var pendingQueue :Queue[RssFeed]) extends Actor {
      val pool = new Array[Fetcher](poolSize)
      var numberProcessed = 0
    
      override def start() :Actor = {
        for (i <- 0 to (poolSize - 1)) {
          val fetcher = new Fetcher(i)
          fetcher.start()
          pool(i) = fetcher
        }
        super.start
      }
    
      def act() {
        for {
          f <- pool
          if (!pendingQueue.isEmpty)
         } {
          pendingQueue.synchronized {
            f ! pendingQueue.dequeue
          }
        }
    
        loop {
          reactWithin(10000L) {
            case id :Int => pendingQueue.synchronized {
              numberProcessed = numberProcessed + 1
              if (!pendingQueue.isEmpty) {
                pool(id) ! pendingQueue.dequeue             
              } else if ((true /: pool) { (done, f) => {
                if (f.getState == Actor.State.Suspended) {
                  f ! Stop
                  done && true
                } else if (f.getState == Actor.State.Terminated) {
                  done && true
                } else {
                  false
                }
              }}) {
                pool foreach { f => {
                  println(f.getState)
                }}
                println("Processed " + numberProcessed + " feeds total.")
                exit
              }
            }
            case TIMEOUT => {
              if (pendingQueue.isEmpty) {
                println("Manager just woke up from timeout with all feeds assigned.")
                pool foreach { f => {
                  if (f.getState == Actor.State.Suspended) {
                    println("Sending Stop to Fetcher " + f.id)
                    f ! Stop
                  }
                }}
                println("Checking state of all Fetchers for termination.")
                if ((true /: pool) { (done, f) => {
                  done && (f.getState == Actor.State.Terminated)
                }}) {
                  exit
                }
              }
            }
          }
        }
      }
    }
    
    class Fetcher(val id :Int) extends Actor {
      var feedsIveDone = 0
      def act() {
        loop {
          react {
            case dbFeed :RssFeed => {
              println("Fetcher " + id + " starting feed")
              //process rss feed here
              feedsIveDone = feedsIveDone + 1
              sender ! id
            }
            case Stop => {
              println(id + " exiting")
              println(feedsIveDone)
              exit
            }
          }
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am spawning new processes in my C# application with System.Diagnostics.Process like this: void
I'm spawning an ssh process in my python script like this: env = {'SSH_ASKPASS':'/home/max/repo/vssh/vssh/vssh.py',
We're spawning URLs in Excel projects tied to a specific project number. Our main
I am programming a http server. There is the main daemon spawning a bunch
1>Project : error PRJ0003 : Error spawning 'rc.exe'.. this is the error i get
My MAIN activity is spawning a child activity that contains a ListView . While
IE8 will sometimes prevent links from spawning if they have target=_blank set. This problem
What is the method of getting the return value from spawning a sub-process within
I've an actor spawning temporary child actors for some jobs, the parent actor is
My Java (Eclipse) application is spawning a child process, monitoring its stdout stream and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.