Jack,
Here's a working example.

Here's the source for the CometActor:


package com.liftcode.comet
import net.liftweb._
import http._
import util._

class Background extends CometActor {
  private val values = new Array[Box[Int]](100)

  // render the information
  def render =
  <div>
    <ul>
      {
        values.zipWithIndex.map{
          case (Full(v), idx) => <li>Item: {idx} is {v}</li>
          case (_, idx) => <li>Item: {idx} <i>Calculating</i></li>
        }
      }
    </ul>
  </div>

  // receive the update and re-render
  override def lowPriority = {
    case (idx: Int, value: Int) =>
      values(idx) = Full(value)
      reRender(false)
  }

  // fork 100 thread
  override def localSetup() {
    super.localSetup()
    values.zipWithIndex.foreach {
      case (_, idx) =>
        (new Thread(
            new Runnable {
              def run() {
                Thread.sleep(10000 + Helpers.randomLong(10000))
                Background.this ! (idx, Helpers.randomInt(1000))
              }
            }
        )).start()
    }
  }
}


Note that the render method cannot block.  You must always render the page
and put placeholders where you will be updating values.

Note also that this code re-renders the entire comet component on each
update.  This is network inefficient.  Please take a look a the Comet Chat
example for how to user partial update which is much more network efficient.

Thanks,

David


On Sun, Sep 27, 2009 at 8:09 PM, jack <jack.wid...@gmail.com> wrote:

>
> I am still having this problem so I will post a simple example. Say I
> want to display a list of the numbers 1 to 100. And suppose I have an
> object Foo and a method bar, which takes an integer and returns an
> integer. And bar takes about 10 seconds to return. So I want to
> display the numbers, run Foo.bar on each of them in the background,
> and then update the display via comet to replace each integer with bar
> of it. I got the Clock example to work and I think I understand what
> is going on there. Could somebody show me how to do this example in
> terms of the Clock example? Or just a few lines of code to suggest how
> to do it?
>
>
>
>
>
>
>
>
> On Sep 18, 12:09 pm, "marius d." <marius.dan...@gmail.com> wrote:
> > On Sep 17, 11:09 pm,jack<jack.wid...@gmail.com> wrote:
> >
> > > I have a CometActor which displays a list of urls and at the same time
> > > launches a bunch of threads each of which gets information about the
> > > urls and then puts messages about that information in a Queue. On each
> > > new tick, the CometActor checks the queue and updates its urls.
> >
> > > The problem is that I am launching the threads from the CometActor and
> > > the page is never coming back. It times out.
> >
> > So the page never gets rendered? I would recommend using actors and
> > not really threads but even with threads it shouldn't impact you. But
> > it also depends on what your code does. Can you post a minimalistic
> > example?
> >
> >
> >
> >
> >
> > > Is there some general principle about launching threads from a
> > > CometActor that might explain this behavior?
> >
>


-- 
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Attachment: comet_foo.tgz
Description: GNU Zip compressed data

Reply via email to