为什么python threading.Thread对象具有“开始"但没有“停止"? [英] Why does the python threading.Thread object has 'start', but not 'stop'?
问题描述
python模块线程具有对象Thread
用于在不同的线程中运行进程和功能.该对象具有start
方法,但没有stop
方法.为什么调用简单的stop
方法无法停止Thread
的原因是什么?我可以想象使用join
方法不方便的情况...
The python module threading has an object Thread
to be used to run processes and functions in a different thread. This object has a start
method, but no stop
method. What is the reason a Thread
cannot be stopped my calling a simple stop
method? I can imagine cases when it is unconvenient to use the join
method...
推荐答案
start
可以是通用且有意义的,因为它只是触发了线程的目标,但是通用stop
会做什么?根据您的线程在做什么,您可能必须关闭网络连接,释放系统资源,转储文件和其他流,或其他任何数量的自定义,重要任务.任何能够以通用方式甚至完成大多数这些事情的系统都会给每个线程增加太多的开销,这是不值得的,并且会变得如此复杂并且遇到特殊情况,几乎不可能工作和.您可以跟踪所有已创建的线程,而无需在主线程中join
将其关闭,然后检查它们的运行状态,并在主线程关闭时将它们传递给某种终止消息.
start
can be generic and make sense because it just fires off the target of the thread, but what would a generic stop
do? Depending upon what your thread is doing, you could have to close network connections, release system resources, dump file and other streams, or any number of other custom, non-trivial tasks. Any system that could do even most of these things in a generic way would add so much overhead to each thread that it wouldn't be worth it, and would be so complicated and shot through with special cases that it would be almost impossible to work with. You can keep track of all created threads without join
ing them in your main thread, then check their run state and pass them some sort of termination message when the main thread shuts itself down though.
这篇关于为什么python threading.Thread对象具有“开始"但没有“停止"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!