擅长:python、mysql、java
<p>如果可能有多个子线程同时运行同一个目标,并希望确保所有子线程都停止,则线程名称非常有用。这似乎是一个有用的概括,在我看来也不太麻烦,但美是在旁观者的眼中:-)。以下内容:</p>
<ul>
<li>启动子线程以打印消息并启动progressbar</li>
<li>使用启动时给定的名称停止子线程。在</li>
</ul>
<p>这是更简单的代码。它做你想要的吗?在</p>
<pre><code>import time, threading
class print_text:
def __init__(self):
pass
def progress(self):
while not self._stop: # Update progress bar
print(".", sep="", end="")
time.sleep(.5)
def echo(self, arg="Default"): # Print message and start progress bar
print(arg)
self._stop = False
threading.Thread(target=self.progress, name="_prog_").start()
def stop(self):
self._stop = True
for t in threading.enumerate():
if t.name == "_prog_":
t.join()
tmp1 = print_text()
tmp1.echo("hello this is text")
time.sleep(10)
tmp1.stop()
print("Done")
</code></pre>