0

As stated in the question above, I wanted to create/implement a Timer to be used in a Maths Test program so that the user can view the timer whilst the system executes various commands. I've tried many timers such as:

import time


s=0
m=0

while s<=60:
    os.system('cls')
    print (m, 'Minutes', s, 'Seconds')
    time.sleep(1)
    s+=1
    if s==60:
        m+=1
        s=0

-

import sys
import os
import time
import datetime

current_time = datetime.datetime.now().time()
current_time = str(current_time)
current_time = current_time[:-7]
print(current_time)

if current_time = #set-the-time:
    #Do something

However the top timer requires the system to sleep. Therefore if I were to implement this in a Maths Test Program, the user will not be able to input any data or perform any actions because the system continuously sleeps. Also when using the second method, again I would have to create some sort of while loop so that a signal/alert is passed when the time limit is reached, but again this will make the system freeze. Is there any way I could implement some sort of timer which allows the program to be run simultaneously???

Hamzah Akhtar
  • 525
  • 5
  • 13
  • 24
  • You'd need to thread out simultaneous processes to do what you want. In your case, you could simply use a queue as the communication channel and do a non-blocking read before each loop execution. Python gives [threading](https://docs.python.org/2/library/threading.html) in the stdlib and [Queue](https://docs.python.org/2/library/queue.html). This doesn't work in all cases, but it seems like you aren't needing much beyond those as far as I can tell. – nerdwaller May 06 '14 at 16:24
  • Is there any chance that you could explain further as to how I could implement that in my code? – Hamzah Akhtar May 06 '14 at 19:06
  • There are several examples around SO, just do a few searches - something like: http://stackoverflow.com/questions/1191374/subprocess-with-timeout may be of interest. – nerdwaller May 06 '14 at 23:36

0 Answers0