擅长:python、mysql、java
<p>在Python中停止线程的最好方法是礼貌地要求它停止。向线程传递新数据的最佳方法是使用<code>Queue</code>模块。在</p>
<p>这两种方法都在<a href="http://eli.thegreenplace.net/2011/05/18/code-sample-socket-client-thread-in-python/" rel="nofollow">the code in this post</a>中使用,它演示了从Python线程进行的套接字通信,但在其他方面与您的问题相关。如果仔细阅读代码,您会注意到:</p>
<ol>
<li>使用<code>threading.Event()</code>,这是由外部的方法调用设置的,线程会定期检查它是否被要求死。在</li>
<li>使用<code>Queue.Queue()</code>向线程传递命令和从线程接收响应。在</li>
</ol>