By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,275 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,275 IT Pros & Developers. It's quick & easy.

How to run a program periodically by using Python

P: 5
I want to run a function periodically by using Python, and I found that
time.sleep() may be of help, however, I think it may not be safe and flexible. Can somebody give me some suggestions on it? Thanks.
Nov 12 '06 #1
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
I want to run a function periodically by using Python, and I found that
time.sleep() may be of help, however, I think it may not be safe and flexible. Can somebody give me some suggestions on it? Thanks.
The Python 2.4.4 docs say: "The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system".
Please tell us more about your application - there may be better ways of doing this. So tell us: Does you app have a GUI? How often is "periodically"? What operating system are you running? Any other details about the function you want to run. Keep posting,
Barton
Nov 12 '06 #2

P: 5
The Python 2.4.4 docs say: "The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system".
Please tell us more about your application - there may be better ways of doing this. So tell us: Does you app have a GUI? How often is "periodically"? What operating system are you running? Any other details about the function you want to run. Keep posting,
Barton
Thanks. The program does not have a GUI, running on a windows system, and I would like to run some functions every hour. The current programs are written in both C and python language, and I packed all the files by Python. Actually, the input files of my programs will be updated every hour externally, so I can run the functions after I detect the input files have been modified. However, if something wrong and the input files have not been updated in some hour, the program will lose synchronization. So I still need to know the time, and run the functions in each hour even the input files have not been updated.
Nov 12 '06 #3

bartonc
Expert 5K+
P: 6,596
I've got a few ideas that I'll work up for you.

Anybody else?
Nov 12 '06 #4

Subsciber123
P: 87
How about something like this:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. next_time=time.time()
  3. while True:
  4.     hourly_function()
  5.     next_time=(next_time+3600) #sets the next occurrence of the
  6.                                    #function to 1 hour later
  7.     while True:
  8.         sleep(60)             #sleeps for a minute
  9.         if time.time()>next_time:
  10.             break
  11.  
This code will not work, however, for more than a day or so, and is only accurate to within about a minute.
Nov 13 '06 #5

P: 5
How about something like this:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. next_time=time.time()
  3. while True:
  4.     hourly_function()
  5.     next_time=(next_time+3600) #sets the next occurrence of the
  6.                                    #function to 1 hour later
  7.     while True:
  8.         sleep(60)             #sleeps for a minute
  9.         if time.time()>next_time:
  10.             break
  11.  
This code will not work, however, for more than a day or so, and is only accurate to within about a minute.
Thanks. But why this code will not work for more than a day? Does the timer will be initialized every day? (Sorry that I am not very good at Python, I will check it later). If it is true, I think I can put the "next_time=time.time()" into the first while block, like this:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. while True:
  3.     next_time=time.time()
  4.     hourly_function()
  5.     next_time=(next_time+3600) #sets the next occurrence of the
  6.                                    #function to 1 hour later
  7.     while True:
  8.         sleep(60)             #sleeps for a minute
  9.         if time.time()>next_time:
  10.             break
  11.  
What do you think of it?
Nov 13 '06 #6

P: 38
On a linux/unix system I would always use Cron for periodic tasks like this instead of running your own Python script to control execution of other Python scripts and c-programs. There are several implementations of Cron on Windows also. Just Google for cron and windows (http://www.cronforwindows.com/ for example).

BR
/Fredrik
Nov 13 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.