开发者

Render in infinity loop

开发者 https://www.devze.com 2022-12-13 18:04 出处:网络
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

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 kn开发者_运维百科ow yet how to use them.

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

Any ideas?


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.


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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消