本文介绍了Python,如果条件为真x的时间量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个条件,该条件仅在a为True的情况下执行超过3秒钟.我希望它像这样工作.
I would like to create a condition that only gets executed if a is True for more than 3 seconds. I would like it to work like this.
if a == True for more than 3 seconds:
dosomething
推荐答案
如果要检查该值3秒钟没有变化.
If you want to check if the value hasn't change for 3 seconds.
import time
id_a_old = id(a)
time.sleep(3)
id_a_new = id(a)
if id_a_old == id_a_new: # assumes that a is initially true
dosomething
由于布尔类型是不可变的,因此如果更改,则对象ID也将更改.
Since bool type is immutable the object id changes if it gets changed.
如果要在3秒钟后检查是否已更改,请执行以下操作.如果任何线程在3秒内更改 a
,它将捕获该信息.
If you want to check if is has changed after 3 seconds do the following. If any thread changes a
within 3 seconds it will capture that.
import time
time.sleep(3)
if a:
dosomething
这篇关于Python,如果条件为真x的时间量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!