Python多线程加载动画

时间:2017-09-27 14:56:23

标签: python multithreading animation

我编写此代码以获取简单的加载动画。动画由专用线程执行但不起作用,仅在程序结束时打印单个视图。我没有看到问题。

import threading
import time
import sys

done = False

class myThread (threading.Thread):
    def __init__(self, threadID, name):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name

    def run(self):
      print ("Starting " + self.name)
      print_loadbar(self.name)
      print ("Exiting " + self.name)

    def print_loadbar(threadName):
      while True:
           if done :
              break
           sys.stdout.write('\r[---    ]')
           sys.stdout.flush()
           time.sleep(0.5)
           sys.stdout.write('\r[    ---]')
           sys.stdout.flush()
           time.sleep(0.5)

thread1 = myThread(1, "Thread-1")
thread1.start()
#MAIN THREAD
c=0
while c<10 :
  time.sleep(1)
  c=c+1
done=True

0 个答案:

没有答案