的Python 2.6.6Python的線程 - 做一些事情,而join()方法
我哈瓦要執行的線程列表和主代碼將等待(Threading.join()),直到他們全部完成。
>>> for thread in threadList:
... thread.start()
>>> for t in threadList:
... t.join()
有沒有辦法打印,如「跑......」,而主代碼等待線程結束的消息?
我想在第二秒後打印同樣的東西(「running ...」),直到線程全部完成。
謝謝。
解決
實施的解決方案:
>>> for thread in threadList:
... thread.start()
>>> string = "running"
>>> while any(t.is_alive() for t in threadList):
... sys.stdout.write("\r%s" % string)
... sys.stdout.flush()
... string = string + "." #this may not be the best way
... time.sleep(0.5)
在我的情況下,額外的時間 「time.sleep(0.5)」 是不是一個問題,但它不是最推薦的。
這產生輸出:
running........
的點之後的「運行」將被打印在一個後,在同一行上,同時有任何線程活着。完全如我所料!
根據@Manuel Jacob和@Alex Hall的回答。
這會導致多達一秒的額外等待時間。 –
謝謝,曼努埃爾雅各!它工作得很好。我會更新問題以顯示我的結果。 – HeitorLima