我基本上是在尝试使用多线程创建“加载条”效果,但是挑战是由于(我至少假设)sys.stdout.flush它确实不会产生我愿意制作的效果;
Starting .... Done!
Starting ....... Done! # Two of the has to work simultaneously
只是为了让一切都清楚,这是程序运行时的(应该是)屏幕截图;
Starting ..
Starting .
Starting ...
Starting ..
Starting .... Done!
Starting ...
我想知道这是否可能?这是我迄今为止尝试过但没有成功的代码;
import time
import sys
import thread
def do_task():
time.sleep(1)
def example_1(n, end_word):
for i in range(n):
do_task()
print '\b.',
sys.stdout.flush()
print end_word + '!'
def func_1():
print ''
sys.stdout.write('Starting ')
example_1(3, 'Done')
def func_2():
print ''
sys.stdout.write('Starting ')
example_1(4, 'Done')
thread.start_new_thread(func_1, ())
thread.start_new_thread(func_2, ())
while 1:
pass