中断计时器

时间:2012-10-25 15:18:56

标签: python multithreading timer interrupt

我正在为个人项目创建一个程序的一部分,我需要一些方面的帮助。

以下是该计划的运作方式:

  1. 用户输入运行时间
  2. 用户输入文本 - 文件已修改
  3. 计时器已启动
  4. 可选用户可以输入"密码"中断计时器
  5. 行动被颠倒
  6. 除了Timer之外,我已经完成了所有已编码的步骤,因为我试图找出执行此操作的最佳方法。理想情况下,我希望计时器显示倒计时,并且如果用户输入了某个密码"计时器被中断,它跳到第5步。

    使用线程最好的方法吗?我过去在线程方面做得不多。我只需要显示计时器,同时还要向用户提供控制权,以防他们输入密码。

    感谢您提供的任何帮助。

    以下是代码:

    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')
    

    正如你所看到的,我还没有添加时间部分。它非常直接,只需将URL添加到文本文件然后关闭它们。如果用户想要原始文件,则调用reverse()。

1 个答案:

答案 0 :(得分:1)

在Windows下,您可以使用msvcrt来请求密钥。要求输入密码实际上更复杂,因为您必须跟踪多个密钥。该程序以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