擅长:python、mysql、java
<p>也许你可以用<code>sched.scheduler</code>来完成,就像这样(n.b.未测试):</p>
<pre><code>import sched, asyncore, time
# Create a scheduler with a delay function that calls asyncore.loop
scheduler = sched.scheduler(time.time, lambda t: _poll_loop(t, time.time()) )
# Add the update timeouts with scheduler.enter
# ...
def _poll_loop(timeout, start_time):
asyncore.loop(timeout, count=1)
finish_time = time.time()
timeleft = finish_time - start_time
if timeleft > timeout: # there was a message and the timeout delay is not finished
_poll_loop(timeleft, finish_time) # so wait some more polling the socket
def main_loop():
while True:
if scheduler.empty():
asyncore.loop(30.0, count=1) # just default timeout, use what suits you
# add other work that might create scheduled events here
else:
scheduler.run()
</code></pre>