Here are a couple of examples:
when a user performs an "event", it kicks off a server side process
that continues executing even when that user signs off.
when a user performs an "event", then after a set period of time (say
two days) then an email is automatically sent to a specified e-mail
address that happens even if the user that triggers the event no
longer has an active session on the server side.
I am attempting to build an auction application in the web2py
framework and would be interested in learning a tecqnique of starting
(and stopping) persistent server-side background processes.
Much thanks
- Define a task table, which is checked on every request
- If the task table is empty then start the background task with
subprocess and store the pid in the task table
- If the task table has an entry but its pid is not active (not in /
proc) then restart the background task and update the pid
- Else the task is running fine
Perhaps you could adapt that to your needs, assuming you are using
Linux.
Richard
On Dec 30 2009, 3:26 pm, Auden RovelleQuartz <oves....@gmail.com>
wrote:
db.define_table('queue',Field('status'),Field('descr'))
In actions
db.queue.insert(status='PENDING',descr='whatever')
In your own scrips that you can run via CRON or via background process
while True:
tasks = db(db.queue.status=='PENDING').select(limitby=(0,10)).first
()
if task:
task.update_record(status='PROCESSING')
db.commit()
try:
#process task
task.update_record(status='COMPLETED')
except:
task.update_record(status='FAILED')
db.commit()
else:
sleep(10) # retry in 10 seconds
My current difficulty is figuring out where to create the persistent
background thread.
- If I spawn it in a controller or model file will it be limited to
10 seconds?
- If I run it in a periodic cron there will be a delay and I will
need to handle the case of multiple threads running simultaneously
- If I use @reboot cron, it prevents web2py from shutting down (it
seems to wait for my thread to quit, which does not know to quit)
Any ideas on these problems? It would be great to have a general task
queue in web2py, like the one on GAE. Although I've never learned how
to use the GAE one, perhaps what we're making could be easier to use.
python web2py.py -S app -M -S thescript
> > > > Much thanks- Hide quoted text -
>
> - Show quoted text -
Michael, are you looking for a particular platform solution or cross
platform?
The trick was that web2py wasn't shutting down when my script was
running, because I had a try-catch that was catching the
KeyboardInterrupt exception. So here's what I did:
In cron/crontab:
@reboot toomim *applications/init/cron/background_work.py
(Thanks to achipa and massimo for fixing @reboot recently!)
In cron/background_work.py:
import time, sys, commands, datetime
# Lock the database so we don't accidentally run two background
# processes at the same time, e.g. if we run web2py twice by
accident
if not db.executesql('select pg_try_advisory_lock(1);')[0][0]:
logging.debug('CRON: FAILED to get background process lock')
raise Exception('Somebody else is running background
processes')
logging.debug('CRON: got lock')
count = 0
while True:
count += 1
logging.debug('Sleeping for the %dth time' % count)
time.sleep(10)
try:
logging.debug('Processing launch queue')
process_launch_queue()
logging.debug('Processing bonus queue')
process_bonus_queue()
except KeyboardInterrupt as e:
logging.debug('Someone killed us.')
raise
except:
logging.debug("Error in background process: %s" %
sys.exc_info()[0])
Note that I explicitly catch the KeyboardInterrupt and re-raise it so
the script dies. If you kill web2py with some other type of exception
you might need to put it in there too.
The other trick is that print statements and exceptions aren't
displayed at the console because this is run as a separate python
process. So you need to use logging.debug from log.py. I don't
remember where I got that file...
> > > > > > any simple example on how to create apersistentbackground process
> > > > > > that runs continuously on the server side no matter what user signs in
> > > > > > or out, and no matter how many concurrent users are on the system?
>
> > > > > > Here are a couple of examples:
>
> > > > > > when a user performs an "event", it kicks off a server side process
> > > > > > that continues executing even when that user signs off.
>
> > > > > > when a user performs an "event", then after a set period of time (say
> > > > > > two days) then an email is automatically sent to a specified e-mail
> > > > > > address that happens even if the user that triggers the event no
> > > > > > longer has an active session on the server side.
>
> > > > > > I am attempting to build an auction application in the web2py
> > > > > > framework and would be interested in learning a tecqnique of starting
> > > > > > (and stopping)persistentserver-side background processes.