3

Is there a way to, for example, print Hello World! every n seconds after x seconds? Looking at this: Run certain code every n seconds, I know how to run a code every n seconds. But I would like to run certain code after x seconds every n seconds.

Need some guidance on how to do this.

Key condition: The program should not lag or sleep. The certain code I am referring to is a function call.

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • Your linked answer http://stackoverflow.com/a/3393759/341744 calls the function `printit` every 5 seconds, without blocking the rest of your program. – ninMonkey Oct 31 '14 at 16:07

3 Answers3

3
import threading
import time

def printit():
  threading.Timer(n, printit).start()
  print "Hello, World!"

threading.Timer(x, printit)
ghostbust555
  • 2,040
  • 16
  • 29
2

Can you just do something like (to run it after 5 seconds, and it'll run every 10 seconds):

import time

time.sleep(5)

while True:
    your code
    time.sleep(10)
AtAFork
  • 376
  • 3
  • 9
0

A: Yes, both ways are possible

For a given task, give a try to use Tkinter internal scheduling, which does not block.

Key methods are:

 .after( n_msec_from_now, aScheduledFuncCALL )  # to plan a call not before n_msec-s
 .after_idle(             aScheduledFuncCALL )  # to plan a call after GUI gets <idle>

more smart options available

import Tkinter as tk

def RunCertainCodeCALL():
    #a CertainCode
    root.after( x * 1000, RunCertainCodeCALL )  # .SET for next round of schedulling

root = tk.Tk()
root.after( x * 1000, RunCertainCodeCALL )      # .SET for 1-st round of schedulling
root.mainloop()                                 # .GUI mainloop()
user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    There's no indication that the OP is using `Tkinter`, is there? – dano Oct 31 '14 at 16:02
  • Dano, how does your question relate to the solution? If you are familiar with Tkinter module internals, you are sure to know, that the benefits of .mainloop() scheduler meet the OP interest. If not, kindly accept there are more ways to reach the OP Task Solution, aren't there? – user3666197 Oct 31 '14 at 16:04
  • 1
    Using a `Tkinter`-based solution would really only make sense if the OP's application is already built around the `Tkinter` mainloop. I don't think that refactoring to make the entire program work within the confines of the mainloop makes sense when something based on `threading.Timer` is available. – dano Oct 31 '14 at 16:09
  • Right, what Tkinter adds to the solution is the scheduling flexibility & elasticity, which threading.Timer() lacks. The code may adapt scheduling delays accordingly upon the application / GUI workloads, which has an immense value in soft-real-time systems. – user3666197 Oct 31 '14 at 16:14
  • I'm not sure what flexibility you're gaining with `after` that isn't possible with `Timer`. Your `after`-based solution looks a whole lot like the `Timer`-based solution, except yours revolves around an event loop instead of threads. In both cases you can adjust the schedule as you go. Can you clarify what you mean? I just don't think any event-loop based solution - asyncio.call_later`, `gobject.timeout_add`, etc. - makes sense here unless you're using that framework to begin with. – dano Oct 31 '14 at 16:31
  • Yes, sure, dano, flexibility was meant to be able to adapt and either hard-schedule or soft-schedule tasks, based on the workload and application context. AFAIK `threading.Timer()` does not allow for any kind of elastic, relaxed scheduling alike `.after_idle()` or `.update_idletasks()` and other context-aware deferred scheduling options alike `.wait_visibility()` or `.wait_variable()` constructs, which make the overall design capable of smarter, elastic non-blocking scheduling and to some extents self-controlled prioritisation possible. – user3666197 Oct 31 '14 at 18:24