Hi All,
I need to call a function for evry 10 secs
how can i achieve this in python

Recommended Answers

All 4 Replies

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.

commented: Good advice and not too much. +13

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

Fuction
10 secs
Fuction
10secs
Fuction
...

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.