python - To execute a function every x minutes: sched or threading.Timer? -
i need program execution of give method every x minutes.
i found 2 ways it: first using sched
module, , second using threading.timer
.
first method:
import sched, time s = sched.scheduler(time.time, time.sleep) def do_something(sc): print "doing stuff..." # stuff sc.enter(60, 1, do_something, (sc,)) s.enter(60, 1, do_something, (s,)) s.run()
the second:
import threading def do_something(sc): print "doing stuff..." # stuff t = threading.timer(0.5,do_something).start() do_something(sc)
what's difference , if there 1 better other, one?
from the sched
documentation:
in multi-threaded environments,
scheduler
class has limitations respect thread-safety, inability insert new task before 1 pending in running scheduler, , holding main thread until event queue empty. instead, preferred approach usethreading.timer
class instead.
Comments
Post a Comment