简体   繁体   English

每n秒重复运行两个不同的功能

[英]Run repeatedly two different functions every n seconds

I have two functions: foo() should start every 5 seconds and bar() should start every 10 seconds. 我有两个函数:foo()应该每5秒启动一次,而bar()应该每10秒启动一次。 code below: 下面的代码:

import threading
import time


def foo():
    while True:
        time.sleep(5)
        print("Call every 5 seconds")
        time.sleep(5)

def bar():
    while True:
        time.sleep(10)
        print("Call every 10 seconds")


tr1 = threading.Thread(target=foo)
tr1.start()
tr2 = threading.Thread(target=bar)
tr2.start()

But I think that this is not a good solution. 但是我认为这不是一个好的解决方案。 What is the best way to do this? 做这个的最好方式是什么? And yes, I think that I have memory leak, should I use garbage collector or anything else? 是的,我认为我有内存泄漏,应该使用垃圾收集器还是其他工具?

PS I hope you could understand what i wrote, because I am not native speaker. 附言:我希望您能理解我写的内容,因为我不是母语人士。

Using the link posted by depperm in the comments 使用depperm在评论中发布的链接

import sched, time
s = sched.scheduler(time.time, time.sleep)

def foo(s):
    print("Call every 5 seconds")
    s.enter(5, 1, foo, (s,))

def bar(s):
    print("Call every 10 seconds")
    s.enter(10, 1, bar, (s,))

s.enter(5, 1, foo, (s,))
s.enter(10, 1, bar, (s,))
s.run()

Your code with minor changes, plus some print statements that suggest that this works alright. 您的代码进行了细微的更改,加上一些print语句表明此方法可以正常工作。 The 'good' thing about this code is that sleep will raise an exception if the argument to it is negative. 这段代码的“好处”是,如果sleep参数为负数,它将引发异常。 Thus, if the codes expected to take less than five or ten seconds respectively take longer then then script will die. 因此,如果预期分别花费不到五秒或十秒的代码花费更长的时间,则脚本将死亡。

import threading
import time
from datetime import timedelta, datetime

def foo(delay=5):
    while True:
        t_start = time.time()
        print("Call every %s seconds" % delay, datetime.now().strftime('%H.%M.%S'))
        t_end = time.time()
        time.sleep(delay-(t_end-t_start))

def bar(delay=10):
    while True:
        t_start = time.time()
        print("Call every %s seconds" % delay, datetime.now().strftime('%H.%M.%S'))
        t_end = time.time()
        time.sleep(delay-(t_end-t_start))

tr1 = threading.Thread(target=foo)
tr1.start()
tr2 = threading.Thread(target=bar)
tr2.start()

Here's the output. 这是输出。

>pythonw -u "temp1.py"
Call every 5 seconds 17.09.51
Call every 10 seconds 17.09.51
Call every 5 seconds 17.09.56
Call every 5 seconds 17.10.01
Call every 10 seconds 17.10.01
Call every 5 seconds 17.10.06
Call every 5 seconds 17.10.11
Call every 10 seconds 17.10.11
Call every 5 seconds 17.10.16
Call every 5 seconds 17.10.21
Call every 10 seconds 17.10.21
Call every 5 seconds 17.10.26
Call every 10 seconds 17.10.31
Call every 5 seconds 17.10.31
Call every 5 seconds 17.10.36

>Process failed to respond; forcing abrupt termination...
>Exit code: 1

I cannot say whether this is the 'best' way of doing this. 我不能说这是否是“最佳”方法。 At least the contents of the functions begin at more or less regular time intervals. 至少函数的内容以或多或少有规律的时间间隔开始。

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

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