的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