0

I want to get time ticker in python in real time. The idea here is I am doing a set of functions again and again, but I want to give a time interval of 10 mins before I execute again. for eg:

rigt now I do this

lst = [1,2,3,4,5]

while lst:

    do something
    lst.pop(0)
    time.sleep(600)

this will just wait for 10 mins and do the loop again.

I want to print in real-time, the seconds left to start the loop again. i.e I want to print

waiting for 600 sec
waiting for 500 sec and so on.

The best would be for it to change just the numbers not printing "waiting for" for every second.

bgporter
  • 35,114
  • 8
  • 59
  • 65
brain storm
  • 30,124
  • 69
  • 225
  • 393

2 Answers2

0

First, I think that you must do a ticker in a separate thread and put "do something" in other thread. To update the tick you can use Clock.tick.

Mihai8
  • 3,113
  • 1
  • 21
  • 31
0

You can use a function like this to print the countdown:

>>> def countdown(seconds):
...     while seconds:
...         sys.stdout.write("\rsleeping for {0}".format(seconds))
...         seconds -= 1
...         time.sleep(1)

(the \r character is a carriage return without line feed -- each time you execute the line that outputs text, it overwrites what was there previously.) so your code would look more like:

while lst:

    # do something
    lst.pop(0)
    countdown(600)
bgporter
  • 35,114
  • 8
  • 59
  • 65
  • It works, but not quite what I want, Thanks though I tried this, >>> lst = [1,2] >>> while lst: ... print "hello\n" ... countdown(6) ... lst.pop(0) I get hello sleeping for 11 hello sleeping for 12 but I want sleeping for 6,(5,4 and so on 1) – brain storm Mar 15 '13 at 22:12