我this post看到它很容易在同一行打印(覆盖先前的内容)的方式如下:的Python:打印到单独的bash线每个线程,在多线程应用程序
print "Downloading " + str(a) " file of " + str(total),
(注最后的逗号)。这将导致
>>> Downloading 1 file of 20
并且每次执行打印时,都会更新同一行。
这适用于单线程应用程序,但它不适用于多线程。
在python 2.7中如何将多个线程打印到自己的终端?
期望的结果会是这个样子:
>>> Thread 1: Downloading 11 file of 20
>>> Thread 2: Downloading 4 file of 87
>>> Thread 3: Downloading 27 file of 32
>>> Thread 4: Downloading 9 file of 21
不应该避免在多线程应用程序中使用全局变量吗? 感谢您的回答,尽管 – Vingtoft
好吧,如果很多线程在相同的全局变量上运行,竞态条件可能会发生,并且需要额外的处理 - 例如使用锁。但在这种简单的情况下,每个线程都在自己的变量上运行,所以没有什么不好的事情会发生 –