<p>这将适用于python3。你知道吗</p>
<p>如果您熟悉asyncio,可以使用event loop<em>call\u later</em>函数。你知道吗</p>
<blockquote>
<p><a href="https://docs.python.org/3/library/asyncio-eventloop.html" rel="nofollow noreferrer">Event loop</a></p>
<p><strong>loop.call_later(delay, callback, *args, context=None)</strong> </p>
<p>Schedule
callback to be called after the given delay number of seconds (can be
either an int or a float).</p>
<p>An instance of asyncio.TimerHandle is returned which can be used to
cancel the callback.</p>
<p>callback will be called exactly once. If two callbacks are scheduled
for exactly the same time, the order in which they are called is
undefined.</p>
<p>The optional positional args will be passed to the callback when it is
called. If you want the callback to be called with keyword arguments
use functools.partial().</p>
<p>An optional keyword-only context argument allows specifying a custom
contextvars.Context for the callback to run in. The current context is
used when no context is provided.</p>
<p>Changed in version 3.7: The context keyword-only parameter was added.
See PEP 567 for more details.</p>
<p>Changed in version 3.8: In Python 3.7 and earlier with the default
event loop implementation, the delay could not exceed one day. This
has been fixed in Python 3.8.</p>
</blockquote>
<p>示例<a href="https://docs.python.org/3/library/asyncio-eventloop.html#display-the-current-date-with-call-later" rel="nofollow noreferrer">(Source)</a>:</p>
<pre><code>import asyncio
import datetime
def display_date(end_time, loop):
print(datetime.datetime.now())
if (loop.time() + 1.0) < end_time:
loop.call_later(1, display_date, end_time, loop)
else:
loop.stop()
loop = asyncio.get_event_loop()
# Schedule the first call to display_date()
end_time = loop.time() + 5.0
loop.call_soon(display_date, end_time, loop)
# Blocking call interrupted by loop.stop()
try:
loop.run_forever()
finally:
loop.close()
</code></pre>