简体   繁体   English

如何每n秒运行一个耗时的进程?

[英]How to run a time consuming process every n seconds?

I need to run a process every n seconds. 我需要每n秒运行一个进程。 I figured Threading.timer is the best way to do it. 我认为Threading.timer是最好的方法。 But when i run my command it doesn't run every n second, but starts running over and over, in a lot less time than the given n and loops unstoppable. 但是,当我运行命令时,它不会每隔n秒运行一次,而是一遍又一遍地开始运行,比给定的n时间要短得多,并且循环无法停止。 Here is my code: 这是我的代码:

#!/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()

So what I want to happen is: It prints Hello world 5 times with 10 seconds brake between each. 所以我想发生的是:它打印Hello World 5次,每次间隔10秒。 But instead it prints it a lot faster and doesn't stop. 但是相反,它可以更快地打印并且不会停止。 Am I missing some parameters, or is the problem with the process I am trying to run? 我是否缺少某些参数,或者我尝试运行的进程是否有问题? I also appreciate any other, simpler methods to run the proess every n seconds. 我还要感谢其他任何更简单的方法,每n秒运行一次proess。

While you appear to be using python2.x (print as a keyword) and thus sleep is a good idea, if you are using py3.4 onwards there is asyncio 虽然您似乎正在使用python2.x(将print作为关键字),因此睡眠是个好主意,但如果您使用的是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()

You can do it just with time in python 您可以在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()

You really want to pause for 10 seconds. 您真的想暂停10秒钟。

time.sleep(brake)

If you did want a timer approach to run all of your operations in a separate thread. 如果您确实希望使用计时器方法在单独的线程中运行所有操作。 You need to separate your print function from the timer function. 您需要将打印功能与计时器功能分开。 However, this will print "Hello World!" 但是,这将打印“ Hello World!”。 5 times all at the same time after 10 seconds has passed. 经过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()

Thanks for all the answers, but in the mean time I figured out the perfect way to do it: 感谢您提供所有答案,但与此同时,我想出了一种完美的方法:

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

Its the only way I know to run a process REPEATEDLY every n seconds. 这是我知道每n秒重复运行一个进程的唯一方法。 It could be done a lot easier simply with time.sleep, but if you have a longer process, you need to reduce the sleeping time by the time your process took to run. 只需使用time.sleep,它可以轻松完成很多操作,但是,如果您的进程较长,则需要在运行过程之前减少睡眠时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM