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.
6 6853
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
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.
I've got a few ideas that I'll work up for you.
Anybody else?
How about something like this: - import time
-
next_time=time.time()
-
while True:
-
hourly_function()
-
next_time=(next_time+3600) #sets the next occurrence of the
-
#function to 1 hour later
-
while True:
-
sleep(60) #sleeps for a minute
-
if time.time()>next_time:
-
break
-
This code will not work, however, for more than a day or so, and is only accurate to within about a minute.
How about something like this: - import time
-
next_time=time.time()
-
while True:
-
hourly_function()
-
next_time=(next_time+3600) #sets the next occurrence of the
-
#function to 1 hour later
-
while True:
-
sleep(60) #sleeps for a minute
-
if time.time()>next_time:
-
break
-
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: - import time
-
while True:
-
next_time=time.time()
-
hourly_function()
-
next_time=(next_time+3600) #sets the next occurrence of the
-
#function to 1 hour later
-
while True:
-
sleep(60) #sleeps for a minute
-
if time.time()>next_time:
-
break
-
What do you think of it?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
by: metaperl |
last post by:
A Comparison of Python Class Objects and Init Files for Program
Configuration
=============================================================================
Terrence Brannon
bauhaus@metaperl.com...
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |