0

I wish to implement timer in python, but could not find any useful articles on that. My main aim is to implement the following logic:

`timer -> 60 seconds to zero
     #do stuff
     if the user selects to manually reset the timer -> start the time loop again
     if user exits -> exit the time loop
 reset the timer and again do above steps`

Am looking for articles / information of the syntax to implement the above logic.

  • 1
    What environment are you working in? Does the user interact with your program via terminal, a GUI, or a browser? – tiwo May 02 '17 at 07:07
  • similar: http://stackoverflow.com/questions/10154568/postpone-code-for-later-execution-in-python-like-settimeout-in-javascript – tiwo May 02 '17 at 07:07
  • Use `time.tme()` ? – t.m.adam May 02 '17 at 07:08
  • @tiwo Am using terminal at the moment but would upgrade it in flask to use it in browser. So, for now i wish to implement it in terminal. – Piyush Nariani May 02 '17 at 07:09

2 Answers2

0

Usually to develop timers in a multithreading enviroment i use the stopit package. You can define a timer -> create a function that call .cancel() on the timer in case the user press cancel button -> catch the timeout exception to restart the timer.

0

Put it in your /usr/bin/stopwatch and give appropriate permission (chmod +x script)

eg: sudo chmod +x /usr/bin/stopwatch

Here is the code

#!/usr/bin/python

from __future__ import print_function

import sys
import time


if len(sys.argv) != 2:
    print("Needs seconds as argument, Eg: stopwatch 10")
else:
    seconds = sys.argv[1]
    for i in range(int(seconds), 0, -1):
        print(str(" "*50), end='\r')
        print(str(i), end='\r')
        sys.stdout.flush()
        time.sleep(1)
    print("Time over {}".format(seconds))

Usage

stopwatch 10
naren
  • 14,611
  • 5
  • 38
  • 45