How to launch a function at regular time intervals ?

David davigier at googlemail.com
Wed Aug 12 17:09:29 EDT 2009


Hi all, I'm trying to launch a function at regular time intervals but
cannot find the way to do it. Here is the code I wrote (time_interval
is a user defined variable in seconds):

while(1)
  timestamp=datetime.now()
 
timestamp_seconds=timestamp.hour*3600+timestamp.minute*60+timestamp.second
  if timestamp_seconds % time_interval == 0: ****Call Function****

This does not work because during the second at which the condition
holds true, there is time to call the function several times. Since I
want to have this function called only once every x seconds, I tried
to add the following condition:

if timestamp_seconds % time_interval ==0 & timestamp.microsecond == 0

But it seems this second condition hardly ever happens (i.e. the
timestamp variable is not updated every microsecond, therefore it can
be 9998 then jump directly to 0003 for instance).

Has anyone run into a similar problem (and solved it) ?

Thanks for your help



More information about the Python-list mailing list