擅长:python、mysql、java
<p>使用线程:</p>
<pre><code>import itertools
import threading
import time
import sys
done = False
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if done:
break
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone! ')
t = threading.Thread(target=animate)
t.start()
#long process here
time.sleep(10)
done = True
</code></pre>
<p>我还对<code>animate()</code>函数做了一些小修改,唯一真正重要的修改是在<code>sys.stdout.write()</code>调用之后添加<code>sys.stdout.flush()</code>。</p>