擅长:python、mysql、java
<p>您可以使用<a href="https://www.programiz.com/python-programming/closure" rel="nofollow noreferrer">closure</a>实现这一点</p>
<pre><code>from time import time
def foo(t, time_to_wait):
def _foo():
if (time() - t) > time_to_wait:
print(f"{time_to_wait} sec passed do something...")
else:
print(f"wait {time_to_wait} seconds")
return _foo
</code></pre>
<hr/>
<pre><code>In [14]: func = foo(time()) # pass the current time
In [15]: func()
wait 5 seconds
In [20]: func()
wait 5 seconds
In [21]: func()
5 sec passed do something...
</code></pre>