【问题标题】:Run function after some time while other runs too [duplicate]一段时间后运行功能,而其他功能也运行[重复]
【发布时间】:2022-02-03 05:00:06
【问题描述】:

我需要两件事情发生:1)一些事情不断发生; 2)时机成熟时会发生一些事情,例如:

while True:
    print('nothing happening')
    if 5_seconds_passed:
       print('five seconds passed')

我希望输出是这样的:

...
nothing happening
nothing happening
nothing happening
nothing happening
five seconds passed
nothing happening
nothing happening
...

【问题讨论】:

    标签: python time schedule


    【解决方案1】:

    如果您希望通过 5 秒,您可以在运行其余代码之前进入循环

    def 5_seconds_passed(oldepoch):
        return time.time() - oldepoch >= 5
    

    oldepoch 是上次运行的时间(在 while 循环开始之前设置)

    source

    【讨论】:

    • 这不是 OP 想要的。
    • 如果您找到类似的答案,请勿转发。
    • @DYZ 我尽量不这样做,但我将如何链接到类似的答案?在cmets?或因为类似的答案而关闭答案(因为我没有这些特权)
    • 是的,然后有较高声誉的人会将这个问题标记为重复。
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2016-10-25
    • 2016-04-20
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多