Run a task at specific intervals in python [duplicate]
Asked Answered
U

5

15

Possible Duplicate:
Suggestions for a Cron like scheduler in Python?

What would be the most pythonic way to schedule a function to run periodically as a background task? There are some ideas here, but they all seem rather ugly to me. And incomplete.

The java Timer class has a very complete solution. Anyone know of a similar python class?

Unifoliolate answered 24/6, 2009 at 14:59 Comment(0)
S
12

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

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

Spathose answered 24/6, 2009 at 15:9 Comment(0)
L
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)
Lovins answered 24/6, 2009 at 17:48 Comment(0)
P
7

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

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

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

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

Peplum answered 24/6, 2009 at 15:9 Comment(2)
"Many programmers try to avoid multi-threaded code, since it is highly bug-prone in imperative programming." Slightly off-topic, but I do not think this is a good reason to avoid multi-threaded. The key is to do it properly and understand the memory consistency and locking semantics fully. I agree that programmers with limited experience of this type of programming should invest heavily in learning and practicing it before employing such techniques in production code. Avoiding it in the era of multi-core programming is not really a viable option if you want to utilise your resources fully.Eveleen
@Eveleen multi-threaded is incredibly hard to get right (and can be just hard to fix), and there are many other ways to use multiple cores beyond threadsShelah
E
7

Not direct response to the question.

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

Response to the question starts here.

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

Evin answered 24/6, 2009 at 15:19 Comment(1)
If you use some "in Python" solution, you have to worry about keeping the script running (after restarts, crashes etc), cron removes the need for this, and is very simple and reliable.Shelah
B
2

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

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

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

Biodegradable answered 24/6, 2009 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.