[ACCEPTED]-Run a task at specific intervals in python-timer
There is a handy event scheduler that might 1 do what you need. Here's a link to the documentation:
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
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.
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
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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.