473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to run a program periodically by using Python

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
6 6853
bartonc
6,596 Expert 4TB
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
excite
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
6,596 Expert 4TB
I've got a few ideas that I'll work up for you.

Anybody else?
Nov 12 '06 #4
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
excite
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
fuffens
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

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

Similar topics

5
by: Kamuela Franco | last post by:
I would like to use a Python script to periodically check to see if a program is still running and if it isn't I want to start it up. Could someone point me on the right path? Thanks in advance....
12
by: Simon John | last post by:
I'm writing a PyQt network client for XMMS, using the InetCtrl plugin, that on connection receives a track length. To save on bandwidth, I don't want to be continually querying the server for...
9
by: Tian | last post by:
I am writing an audio game using Python. in this game you can apply some sound effects for the clips you have recorded. I want to make this function extensible. I want user to be able to add new...
5
by: James | last post by:
Hi, I working on an ASP .NET project, in this project I need to link my project with another program to retrieve data, how can I do that? I have access to source code of the other program where...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
14
by: fdu.xiaojf | last post by:
Hi, I have a program which will continue to run for several days. When it is running, I can't do anything except waiting because it takes over most of the CUP time. Is it possible that the...
6
by: yaru22 | last post by:
I'd like to create a program that validates bunch of urls against the w3c markup validator (http://validator.w3.org/) and store the result in a file. Since I don't know network programming, I...
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.