【发布时间】:2017-05-28 20:15:30
【问题描述】:
我想要实现的是每 n 秒重复一个函数,而我在后台有第二个计时器像时钟一样运行,所以当 n 分钟过去时,脚本终止。我知道treading.timer,但是当我有2个定时器时它不起作用。欢迎任何想法,因为我对 python 还很陌生。
【问题讨论】:
标签: python python-2.7 timer
我想要实现的是每 n 秒重复一个函数,而我在后台有第二个计时器像时钟一样运行,所以当 n 分钟过去时,脚本终止。我知道treading.timer,但是当我有2个定时器时它不起作用。欢迎任何想法,因为我对 python 还很陌生。
【问题讨论】:
标签: python python-2.7 timer
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)
或者如果您有任何其他背景源,最好知道它是什么。
【讨论】: