【问题标题】:repeat a function every n seconds while counting down n minutes?在倒计时 n 分钟时每 n 秒重复一次功能?
【发布时间】:2017-05-28 20:15:30
【问题描述】:

我想要实现的是每 n 秒重复一个函数,而我在后台有第二个计时器像时钟一样运行,所以当 n 分钟过去时,脚本终止。我知道treading.timer,但是当我有2个定时器时它不起作用。欢迎任何想法,因为我对 python 还很陌生。

【问题讨论】:

    标签: python python-2.7 timer


    【解决方案1】:
    import time
    import datetime
    
    def somefunction(x):
        return x
    
    
    x = True
    while x:
        a = datetime.datetime.now().hour
    
        if a < 22:
            time.sleep(1)
            somefunction(x)
    
        else:
            x = False
    

    或:

    script1.py

    import datetime
    
    a = datetime.datetime.now().hour
    

    script2.py

    from script1 import a
    import time
    
    
    
    def somefunction(x):
        return x
    
    
    
    while a < 22: #execute function as long it is less than 22.00, else stop
    
        time.sleep(1)
        somefunction(x)
    

    或者如果您有任何其他背景源,最好知道它是什么。

    【讨论】:

    • 这如何与其他代码并行(在后台)运行?
    猜你喜欢
    • 2019-09-27
    • 2018-01-06
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多