擅长:python、mysql、java
<p>您想要的是使您的线程<code>daemon</code>,因此当您得到结果并完成main时,另一个正在运行的线程将被迫完成。您可以将<code>daemon</code>关键字更改为<code>True</code>:</p>
<pre class="lang-py prettyprint-override"><code>tr = threading.Thread(daemon=True)
</code></pre>
<p>从<a href="https://docs.python.org/3/library/threading.html#thread-objects" rel="nofollow noreferrer">^{<cd4>}</a>文档:</p>
<blockquote>
<p>The significance of this flag is that the entire Python program exits
when only daemon threads are left.</p>
</blockquote>
<p>尽管:</p>
<blockquote>
<p>Daemon threads are abruptly stopped at shutdown. Their resources (such
as open files, database transactions, etc.) may not be released
properly. If you want your threads to stop gracefully, make them
non-daemonic and use a suitable signalling mechanism such as an <a href="https://docs.python.org/3/library/threading.html#threading.Event" rel="nofollow noreferrer"><code>Event</code></a>.</p>
</blockquote>
<p>我对<code>Event</code>没有什么特别的经验,所以不能详细说明。请随意点击链接继续阅读。你知道吗</p>