线程与线程
Python 中的 threading
和 thread
模块有什么区别?
What's the difference between the threading
and thread
modules in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Python 中的 threading
和 thread
模块有什么区别?
What's the difference between the threading
and thread
modules in Python?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
在 Python 3 中,
thread
已重命名为_thread
。它是用于实现线程的基础设施代码,普通的 Python 代码不应该靠近它。_thread
公开了底层操作系统级进程的相当原始的视图。这几乎不是您想要的,因此在 Py3k 中重命名以表明它实际上只是一个实现细节。threading
添加了一些额外的自动记帐,以及一些便利的实用程序,所有这些使其成为标准 Python 代码的首选。注意:正如Jeril的替代答案中提到的,当考虑在Python中使用线程来执行后台任务时,也采取将concurrent.futures.ThreadPoolExecutor视为比线程更高级别的API:https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
In Python 3,
thread
has been renamed to_thread
. It is infrastructure code that is used to implementthreading
, and normal Python code shouldn't be going anywhere near it._thread
exposes a fairly raw view of the underlying OS level processes. This is almost never what you want, hence the rename in Py3k to indicate that it is really just an implementation detail.threading
adds some additional automatic accounting, as well as several convenience utilities, all of which makes it the preferred option for standard Python code.Note: as mentioned in Jeril's alternate answer, when considering the use of threads in Python for execution of background tasks, also take a look at
concurrent.futures.ThreadPoolExecutor
as an even higher level API thanthreading
: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutorthreading
只是一个与thread
接口的更高级别模块。请参阅此处的
线程
文档:http://docs.python.org/library/threading。 html
threading
is just a higher level module that interfacesthread
.See here for the
threading
docs:http://docs.python.org/library/threading.html
如果我没记错的话,
线程
允许您将函数作为单独的线程运行,而使用线程
您必须< /s> 创建一个类,但获得更多功能。编辑:这并不完全正确。
threading
模块提供了创建线程的不同方式:threading.Thread(target=function_name).start()
threading.Thread
的子类> 使用您自己的run()
方法,然后启动它If I'm not mistaken,
thread
allows you to run a function as a separate thread, whereas withthreading
youhave tocreate a class, but get more functionality.EDIT: This is not precisely correct.
threading
module provides different ways of creating a thread:threading.Thread(target=function_name).start()
threading.Thread
with your ownrun()
method, and start itPython 中还有另一个库可以用于线程并且工作得很好。
该库名为 concurrent.futures。这使我们的工作更加轻松。
它具有 线程池 和 进程池。
下面给出了一个见解:
ThreadPoolExecutor 示例
另一个示例
There is another one library in Python which can used for threading and works perfectly.
The library called concurrent.futures. This makes our work easier.
It has for thread pooling and Process pooling.
The following gives an insight:
ThreadPoolExecutor Example
Another example
模块“Thread”将线程视为一个函数,而模块“threading”则以面向对象的方式实现,即每个线程对应一个对象。
The module "Thread" treats a thread as a function, while the module "threading" is implemented in an object oriented way, i.e. every thread corresponds to an object.