5
Contributors
4
Replies
13
Views
5 Years
Discussion Span
Last Post by 3e0jUn
1

Generally, you would store the next time you want to execute, i.e. now+10 seconds,
using a loop,
call the function,
upon the return get the time and calculate the difference between it and the next time and sleep that amount of time,
update the next time, (+10 seconds), and loop again.

Also, Python has a schedule module, as do most operating systems. On Linux you can use cron to execute the program every 10 seconds, if you want to continue forever.

Edited by woooee: n/a

Votes + Comments
Good advice and not too much.
0

What do you mean by the seconds do you mean this:

Fuction
10 secs
Fuction
10secs
Fuction
...

0

Like Woooee suggested, cron is usually the best way to schedule something like this reliably. python does have a 'sleep()' function to make this easy though!

Example:

#!/usr/bin/python
import time
while True:
    print "x"
    time.sleep(10)

I hope this helps!
-Jeo

0

You can always use the threading module for scheduling such functions trivially. I.e.

def worker(self, name): 
# check if we need to exit
    while not self._exitflag:
        time.sleep(self._nap_time)
        self._threadLock.acquire()

        if self._need_to_do_job:
            # perform jobs
            pass

        self._threadLock.release()

Though when you .join() the function, you will need to wait for the time specified in your self._nap_time variable.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.