Hi tommy,

I don't know if it's a good ideia to put this verification
(session.enginestarted=True) in a session var. Because the client can close
the browser, open the link again and start another thread...

I have here a small example where the client can start just one thread.

*In a model:*
db.define_table('test',SQLField('counter','integer'),SQLField("doing_something",
"boolean", notnull=True, default='False'))

*In a view:* (default/test.html)
{{extend 'layout.html'}}
<h1>This is the default/test.html template</h1>
<form><input id='source' type='text' value={{=old}}
readonly="readonly"></form>
<button id='botao', name='botao',
onclick="ajax('add_more',[],'destination');"> DO SOMETHING!
</button><br><br>

<div id="destination">...</div> <!-- results should appear here -->

*In a controller:*
def add_more():
    import time
    
doing_something=db(db.test.id==1).select(db.test.doing_something)[0].doing_something
# get the value of doing_something
    if not doing_something: # if it is = false
        db(db.test.id==1).update(doing_something=True) # doing something
now...
        db.commit() # very important, commit it
        time.sleep(10) # doing something....
        old=db(db.test.id==1).select(db.test.counter)[0] # just adding... :)
        db(db.test.id==1).update(counter=old.counter+1) # just adding... :)
        db(db.test.id==1).update(doing_something=False) # Ok... done
something...
        return 'OK! You can do it again!'
    else:
        return 'you are already doing something...'

def test():
    return dict(old=db(db.test.id==1).select(db.test.counter)[0].counter)

*There is a detail in web2py_ajax.html that you must change:*
function ajax(u,s,t) {
  var query="";
  for(i=0; i<s.length; i++) {
     if(i>0) query=query+"&";

query=query+encodeURIComponent(s[i])+"="+encodeURIComponent(document.getElementById(s[i]).value);
  }
  $.ajax({*async: false*, type: "POST", url: u, data: query, success:
function(msg) { document.getElementById(t).innerHTML=msg; } });
}

with async = false your client should be able to run just a single thread
per time, so it avoids repetitious clicks opening a lot of threads.

I hope that this example helps you!

Regards,

Tito Garrido :)

On Sun, Oct 26, 2008 at 5:01 PM, tommy <[EMAIL PROTECTED]> wrote:

>
> Massimo,
>
> Here is my code,
>
> I use GUI button to start my service (StartEngine, in the background,
> I want to retrieve the infomation from another application and save it
> in my database for every couple of munites.  after I starting my start
> engine service, I can go to other screen. and the background service
> keeps runing.
>
>
> I can create the thread now, but got an error :SQLITE object created
> in a thread can only used  in that same thread.
>
> def enginestart():
>    menu_setup()
>  #   engine.start()
>    session.starttimer=True
>
>    symbols=db(db.symbols.id>0).select()
>
>   # for symbol in symbols:
>   #     engine.addMarket(symbol.symbol_Name,
> symbol.description,symbol.tick)
>
>
>    session.flash='engine stared'
>    session.enginestarted=True
>    db(db.historyprice.id >0).delete()
>  #   tt=threading.Timer(2, gethistoryprice(db))
>  #   tt.start()
>    t=Gethistoryprice()
>    t.start()
>    redirect(URL(r=request,c='engine',f='enginemaint'))
>    return dict()     session.starttimer=True
>
>    symbols=db(db.symbols.id>0).select()
>
>   # for symbol in symbols:
>   #     engine.addMarket(symbol.symbol_Name,
> symbol.description,symbol.tick)
>
>
>    session.flash='engine stared'
>    session.enginestarted=True
>    db(db.historyprice.id >0).delete()
>    t=Gethistoryprice(db)
>    t.start()
>    redirect(URL(r=request,c='engine',f='enginemaint'))
>    return dict()
>
>
>
> class  Gethistoryprice(threading.Thread):
>  def __init__(self, db):
>         threading.Thread.__init__(self)
>
>         self.stopped=0
>         self.db=db
>
>
>     def run(self):
>       while not self.stopped:
>          mysym=self.db(db.symbols.id>0).select()
>          for symbol in mysym:
>              #  p=engine.getlastprice(symbol)
>              p=random.randrange(10.00,60.00)
>              now=datetime.datetime.now()
>              db.historyprice.insert(symbol_Name=symbol.symbol_Name,
> timestamp= now, price=p)
>
>     def stop(self):
>         self.stopped=1
>
>
>
> On Oct 26, 2:05 pm, mdipierro <[EMAIL PROTECTED]> wrote:
> > This app does that. You upload a video and it is converted in
> > background (like at youtube) in a separate process (not a thread, a
> > process although the process my have threads). The visitor can later
> > check the conversion was completed. It includes documentation:
> >
> > http://mdp.cti.depaul.edu/appliances/default/show/38
> >
> > Massimo
> >
> > On Oct 26, 2:00 pm, Keith Edmunds <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > On Sun, 26 Oct 2008 11:55:08 -0700 (PDT), [EMAIL PROTECTED]
> > > said:
> >
> > > > 1) what other background tasks do people envisage?
> >
> > > A regular display update via Ajax (but that could probably be demanded
> by
> > > the client rather than pushed)
> >
> > > > 2) how should you do it in web2py?
> >
> > > Pass.
> >
> > > --
> > > Keith Edmunds- Hide quoted text -
> >
> > - Show quoted text -
> >
>


-- 
Linux User #387870
.........____
.... _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:_______

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to