[ACCEPTED]-Run a task at specific intervals in python-timer

Accepted answer
Score: 12

There is a handy event scheduler that might 1 do what you need. Here's a link to the documentation:

http://docs.python.org/library/sched.html

Score: 9

try the multiprocessing module.

from multiprocessing import Process
import time

def doWork():
    while True:
        print "working...."
        time.sleep(10)



if __name__ == "__main__":
    p = Process(target=doWork)
    p.start()

    while True:
        time.sleep(60)

0

Score: 7

Not direct response to the question.

On Linux/Unix 6 operating system there are few ways to do 5 so and usually I just write my program / script 4 normally and then add it to cron or something 3 similar (like launchd on OS X)

Response to the question 2 starts here.

Use standard python sched module 1 - standard library documentation describes some nifty solutions.

Score: 6

Many programmers try to avoid multi-threaded 9 code, since it is highly bug-prone in imperative 8 programming.

If you want to a scheduled task 7 in a single-threaded environment, then you 6 probably need some kind of "Reactor". You 5 may want to use a ready-made one like Twisted's.

Then 4 it would be a basic function provided by 3 your reactor, for example (with pygame):

pygame.time.set_timer 2 - repeatedly create an event on the event 1 queue

Score: 2

Python has a Timer class in threading module 7 but that is one-shot timer, so you would 6 be better doing something as you have seen 5 links. http://code.activestate.com/recipes/65222/

Why do you think that is ugly, once 4 you have written such a class usage will 3 be as simple as in java.

if you are using 2 it inside some GUI e.g. wxPython than it 1 has wx.Timer which you can directly use

More Related questions