【问题标题】:Running a Python script for a user-specified amount of time在用户指定的时间内运行 Python 脚本
【发布时间】:2010-05-14 04:08:55
【问题描述】:

我今天刚开始学习 Python。我一直在阅读 Byte of Python。现在我有一个涉及时间的 Python 项目。我在 Byte of Python 中找不到任何与时间有关的内容,所以我会问你:

如何在用户指定的时间内运行块然后中断?

例如(在一些伪代码中):

time = int(raw_input('Enter the amount of seconds you want to run this: '))
while there is still time left:
    #run this block

甚至更好:

import sys
time = sys.argv[1]
while there is still time left:
    #run this block

【问题讨论】:

    标签: python


    【解决方案1】:

    如果您使用的是 Linux,并且想要中断一个长时间运行的进程,请使用 signal

    import signal, time
    
    def got_alarm(signum, frame):
        print 'Alarm!'
    
    # call 'got_alarm' in two seconds:
    signal.signal(signal.SIGALRM, got_alarm)
    signal.alarm(2)
    
    print 'sleeping...'
    time.sleep(4)
    
    print 'done'
    

    【讨论】:

      【解决方案2】:

      我建议生成另一个thread,使其成为daemon thread,然后是sleeping,直到您希望任务终止。例如:

      from time import sleep
      from threading import Thread
      
      def some_task():
          while True:
              pass
      
      t = Thread(target=some_task)  # run the some_task function in another
                                    # thread
      t.daemon = True               # Python will exit when the main thread
                                    # exits, even if this thread is still
                                    # running
      t.start()
      
      snooziness = int(raw_input('Enter the amount of seconds you want to run this: '))
      sleep(snooziness)
      
      # Since this is the end of the script, Python will now exit.  If we
      # still had any other non-daemon threads running, we wouldn't exit.
      # However, since our task is a daemon thread, Python will exit even if
      # it's still going.
      

      当所有非守护线程都退出时,Python 解释器将关闭。因此,当您的主线程退出时,如果唯一运行的其他线程是您在单独的守护线程中运行的任务,那么 Python 将退出。如果您希望能够直接退出而不用担心手动导致它退出并等待它停止,这是一种在后台运行某些东西的便捷方式。

      换句话说,与在 for 循环中使用 sleep 相比,这种方法的优势在于,在这种情况下,您必须以将任务分解为离散块的方式对任务进行编码,然后每隔一段时间检查一次你的时间是否到了。这对于您的目的可能很好,但它可能会出现问题,例如每个块是否需要大量时间,从而导致您的程序运行时间比用户输入的时间长得多,等等。这对您来说是否是个问题取决于您正在编写的任务,但我想我会提到这种方法,以防它对您更好。

      【讨论】:

      • 对不起,你能评论一下吗?我还没有完全冒险进入线程和睡眠。根据我对您的回答的理解:从睡眠中醒来会破坏线程?
      • @Rob:我在代码中添加了更多解释和几个 cmets。如果有什么特别不清楚的地方,请告诉我。
      • 其实你已经说得很清楚了。这似乎比重复检查时间更有效。非常感谢
      • t.start 放在inputsleep 语句之间会不会更好一些,这样在用户思考并输入所需时间时守护程序不会运行?
      【解决方案3】:

      试试time.time(),它将当前时间返回为自称为 epoch 的设定时间(对于许多计算机而言是 1970 年 1 月 1 日午夜)以来的秒数。这是使用它的一种方法:

      import time
      
      max_time = int(raw_input('Enter the amount of seconds you want to run this: '))
      start_time = time.time()  # remember when we started
      while (time.time() - start_time) < max_time:
          do_stuff()
      

      因此,只要自开始以来的时间小于用户指定的最大值,我们就会循环。这并不完美:最值得注意的是,如果do_stuff() 需要很长时间,我们不会停止,直到它完成并且我们发现我们已经超过了截止日期。如果您需要能够在时间过去后立即中断正在进行的任务,那么问题就会变得更加复杂。

      【讨论】:

      • 实际上,我确实需要能够中断正在进行的任务。但这不是像if (time.time() - start_time) &gt; max_time: break 这样的简单修改吗?如果我错了,请纠正我
      • 当然!这会很好 - 你只需要确保在小块执行之间运行该测试。
      • 这种方法不可扩展,因为 while 条件的开销。假设我想调用 do_Stuff n 次,使得 n >>> max_time,这种方法会很困难。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多