Rev22
Rev22

Reputation: 27

Interrupting a timer

I'm creating part of a program right now for a personal project and I need some help on one aspect of it.

Here is how the program works:

  1. User enters the amount of time to run
  2. User enters the text - Files are modified
  3. Timer is started
  4. optional User can enter "password" to interrupt the timer
  5. Actions are reversed

I have all of the steps coded except the Timer because I'm trying to figure out the best way to do this. Ideally, I'd like the timer to be displaying a countdown, and if the user enters a certain "password" the timer is interrupted and it skips to step number 5.

Would the best way to do this be with a thread? I haven't worked much with threads in the past. I just need someway for the timer to be displayed while also giving control back to the user in case they want to enter that password.

Thanks for any help you provide.

Here's the code:

import time
import urllib
import sys

def restore():
     backup = open(r'...backupfile.txt','r')
     text = open(r'...file.txt', 'w+')
     text.seek(0)

     for line in backup:
         text.write(line)

     backup.close()
     text.close()

text = open(r'...file.txt', 'a+')
backup = open(r'...backupfile.txt','w+')
text.seek(0)

for line in text:
    backup.write(line)

backup.close()

while True:
    url = raw_input('Please enter a URL: ')
    try:
        if url[:7] != 'http://':
            urllib.urlopen('http://' + url) 
        else:
            urllib.urlopen(url)
    except IOError:
        print "Not a real URL"
        continue 

    text.write(url)

    while True:
        choice = raw_input('Would you like to enter another url? (y/n): ')
        try:
            if choice == 'y' or choice == 'n':
                break
        except:
            continue

        if choice == 'y':
            text.seek(2)
            continue

        elif choice == 'n':
            while True:
                choice = raw_input('Would you to restore your file to the original backup (y/n): ')
                try:
                    if choice == 'y' or choice == 'n':
                        break
                except:
                    continue

            if choice == 'y':
                text.close()
                restore()
                sys.exit('Your file has been restored')
            else:
                text.close()
                sys.exit('Your file has been modified')

As you can see, I haven't added the timing part yet. It's pretty straight forward, just adding urls to a text file and then closing them. If the user wants the original file, reverse() is called.

Upvotes: 3

Views: 888

Answers (1)

RParadox
RParadox

Reputation: 6851

Under Windows you can use msvcrt to ask for a key. Asking for a password is actually more complex, because you have to track several keys. This program stops with F1.

import time
import msvcrt

from threading import Thread
import threading

class worker(Thread):

    def __init__(self,maxsec):
        self._maxsec = maxsec
        Thread.__init__(self)
        self._stop = threading.Event()

    def run(self):
        i = 1
        start = time.time()
        while not self.stopped():
            t = time.time()
            dif = t-start
            time.sleep(1) # you want to take this out later (implement progressbar)

            # print something once in a while
            if i%2==0: print '.',

            #check key pressed
            if msvcrt.kbhit():
                if ord(msvcrt.getch()) == 59:
                    self.stop()


            #do stuff

            # timeout
            if dif > self._maxsec:
                break

            i+=1

    def stop(self):
        print 'thread stopped'
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

print 'number of seconds to run '
timeToRun = raw_input()

#input files
#not implemented

#run
w = worker(timeToRun)
w.run()

#reverse actions

Upvotes: 1

Related Questions