在项目中经常遇到间隔执行某个命令
但不能使用time.sleep()这种阻塞函数
试试下面的方法:
import moment
last_time1 = moment.now()
last_time2 = moment.now()
def dingshi():
global last_time1
global last_time2
if last_time1.__lt__(moment.now()):
last_time1 = moment.now().add(seconds=2)
# 任务
print("1=", last_time1)
if last_time2.__lt__(moment.now()):
last_time2 = moment.now().add(seconds=5)
print("2=", last_time2)
while True:
dingshi()