views:

115

answers:

4

Question for Python 2.6

I would like to create an simple web application which in specified time interval will run a script that modifies the data (in database). My problem is code for infinity loop or some other method to achieve this goal. The script should be run only once by the user. Next iterations should run automatically, even when the user leaves the application. If someone have idea for method detecting apps breaks it would be great to show it too. I think that threads can be the best way to achive that. Unfortunately, I just started my adventure with Python and don't know yet how to use them.

The application will have also views showing database and for control of loop script.

Any ideas?

+1  A: 

i think you'd want to use cron. write your script, and have cron run it every X minutes / hours.

if you really want to do this in Python, you can do something like this:

while(True):
    <your app logic here>
    sleep(TIME_INTERVAL)
andylei
Work incorrect in my case. Sleep method increase time of script *execution*. Is there any *corn* writed in python?
Konrad
why do you need cron to be written in python?
andylei
I don't need cron (in Python) but simple solution with functionality like cron. I would like to have all the application controled by Python code and don't care if cron is instaled on server or not. But it seems that cron is the best solution...
Konrad
if you're on google app engine: http://code.google.com/appengine/docs/python/config/cron.html
andylei
A: 

Can you use cron to schedule the job to run at certain intervals? It's usually considered better than infinite loops, and was designed to help solve this sort of problem.

FrustratedWithFormsDesigner
A: 

There's a very primitive cron in the Python standard library: import sched. There's also threading.Timer.

But as others say, you probably should just use the real cron.

Jason Orendorff
Could you tell more? How to use them in my case? Is this method *prevent script timeout*?
Konrad
No. If you're on GAE you definitely want Kevin's answer.
Jason Orendorff
+4  A: 

You mentioned that you're using Google App Engine. You can schedule recurring tasks by placing a cron.yaml file in your application folder. The details are here.

Update: It sounds like you're not looking for GAE-specific solutions, so the more general advice I'd give is to use the native scheduling abilities of whatever platform you're using. Cron jobs on a *nix host, scheduled tasks on Windows, cron.yaml on GAE, etc.

In your other comments you've suggested wanting something in Python that doesn't leave your script executing, and I don't think there's any way to do this. Some process has to be responsible for kicking off whatever it is you need done, so either you do it in Python and keep a process executing (even if it's just sleeping), or you use the platform's scheduling tools. The OS is almost guaranteed to do a better job of this than your code.

Iceman
I thought that there would have to be an answer along these lines... I think we have a winner!
Skilldrick
Sorry, not yet :)
Konrad