【问题标题】:How to run a time consuming process every n seconds?如何每 n 秒运行一次耗时的进程?
【发布时间】:2017-03-27 18:14:04
【问题描述】:

我需要每 n 秒运行一个进程。我认为 Threading.timer 是最好的方法。但是当我运行我的命令时,它不会每隔 n 秒运行一次,而是开始一遍又一遍地运行,比给定的 n 时间短得多,并且循环不可停止。这是我的代码:

#!/usr/bin/python

import threading
import time

brake = 10
k = int(5)

def printit():
    for x in range(k):
        threading.Timer((int(brake)), printit).start()
        print "Hello World!"  
        #i have a longer process here, it takes a few seconds to run
        #i give more than double of the time needed to run it
printit()

所以我想要发生的是:它打印 Hello world 5 次,每次之间有 10 秒的刹车。但是相反,它的打印速度要快得多并且不会停止。 我是否缺少一些参数,或者我尝试运行的过程有问题?我也很欣赏任何其他更简单的方法来每 n 秒运行一次进程。

【问题讨论】:

    标签: python python-multithreading


    【解决方案1】:

    虽然您似乎在使用 python2.x(打印作为关键字),因此 sleep 是个好主意,但如果您使用 py3.4 及更高版本,则存在 asyncio

    import asyncio
    
    def do_task(end_time, loop):
        print('hello world!')
        if (loop.time() + 1.0) < end_time:
            loop.call_later(1, do_task, end_time, loop)
        else:
            loop.stop()
    
    loop = asyncio.get_event_loop()
    
    end_time = loop.time() + 10.0
    loop.call_soon(do_task, end_time, loop)
    
    loop.run_forever()
    loop.close()
    

    【讨论】:

      【解决方案2】:

      你可以在 python 中使用time 来做到这一点

      import time
      
      k = int(5)
      
      def printit():
          for x in range(k):
              time.sleep(5)
              print "Hello World!"
              #i have a longer process here, it takes a few seconds to run
              #i give more than double of the time needed to run it
      printit()
      

      【讨论】:

      • 很好,但正如我提到的,我的进程需要几秒钟才能运行,所以如果我运行它,它会等待 5 秒,我的进程会不会(需要时间),所以下次它启动是 5 秒 + 运行该进程所用的时间。
      【解决方案3】:

      你真的想暂停 10 秒。

      time.sleep(brake)
      

      如果您确实希望使用计时器方法在单独的线程中运行所有操作。您需要将打印功能与计时器功能分开。但是,这将打印“Hello World!” 10 秒后同时进行 5 次。

      import threading
      import time
      
      brake = 10
      k = 5
      
      def printit():
          for x in range(k):
              threading.Timer(brake, long_process, args=('Hello World!',)).start()
      
      def long_process(item):
          print(item)
          #i have a longer process here, it takes a few seconds to run
          #i give more than double of the time needed to run it
      
      
      printit()
      

      【讨论】:

        【解决方案4】:

        感谢所有答案,但与此同时,我找到了完美的方法:

        import time
        
        a=2
        b=5
        
        for x in range(a):
            start = time.time()
            print "Hello World!"
            time.sleep(3)
            run = (-(start)+time.time())
            time.sleep(int(b)-(run))
        

        这是我知道每 n 秒重复运行一个进程的唯一方法。只需使用 time.sleep 就可以轻松得多,但如果您的进程较长,则需要通过进程运行时间来减少睡眠时间。

        【讨论】:

          猜你喜欢
          • 2020-07-15
          • 2013-02-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-24
          • 2022-12-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多