Python 线程有问题吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:24:59  来源:igfitidea点击:

Are Python threads buggy?

pythonmultithreading

提问by Tyler

A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy - enough to avoid using altogether. What can said about this rumor?

一位可靠的编码朋友告诉我,Python 当前的多线程实现存在严重缺陷——足以避免完全使用。这个谣言能说什么呢?

回答by Avner

Python threads are good for concurrent I/O programming. Threads are swapped out of the CPU as soon as they block waiting for input from file, network, etc. This allows other Python threads to use the CPU while others wait. This would allow you to write a multi-threaded web server or web crawler, for example.

Python 线程适用于并发 I/O 编程。一旦线程阻塞等待来自文件、网络等的输入,它们就会被交换出 CPU。这允许其他 Python 线程在其他线程等待时使用 CPU。例如,这将允许您编写多线程网络服务器或网络爬虫。

However, Python threads are serialized by the GILwhen they enter interpreter core. This means that if two threads are crunching numbers, only one can run at any given moment. It also means that you can't take advantage of multi-core or multi-processor architectures.

但是,Python 线程在进入解释器核心时会被GIL序列化。这意味着如果两个线程正在处理数字,那么在任何给定时刻只有一个线程可以运行。这也意味着您无法利用多核或多处理器架构。

There are solutions like running multiple Python interpreters concurrently, using a C based threading library. This is not for the faint of heart and the benefits might not be worth the trouble. Let's hope for an all Python solution in a future release.

有一些解决方案,例如使用基于 C 的线程库并发运行多个 Python 解释器。这不适合胆小的人,而且好处可能不值得麻烦。让我们希望在未来的版本中提供一个全 Python 的解决方案。

回答by Hamish Downer

The standard implementation of Python (generally known as CPython as it is written in C) uses OS threads, but since there is the Global Interpreter Lock, only one thread at a time is allowed to run Python code. But within those limitations, the threading libraries are robust and widely used.

Python 的标准实现(通常称为 CPython,因为它是用 C 编写的)使用操作系统线程,但由于有全局解释器锁,一次只允许一个线程运行 Python 代码。但是在这些限制范围内,线程库是健壮的并且被广泛使用。

If you want to be able to use multiple CPU cores, there are a few options. One is to use multiple python interpreters concurrently, as mentioned by others. Another option is to use a different implementation of Python that does not use a GIL. The two main options are Jythonand IronPython.

如果您希望能够使用多个 CPU 内核,有几个选项。一种是同时使用多个 python 解释器,正如其他人所提到的。另一种选择是使用不使用 GIL 的不同 Python 实现。两个主要选项是JythonIronPython

Jython is written in Java, and is now fairly mature, though some incompatibilities remain. For example, the web framework Django does not run perfectly yet, but is getting closer all the time. Jython is great for thread safety, comes out better in benchmarksand has a cheeky message for those wanting the GIL.

Jython 是用 Java 编写的,现在已经相当成熟,但仍然存在一些不兼容性。例如,Web 框架Django 还没有完美运行,但一直在接近。Jython非常适合线程安全在基准测试中表现更好,并且为那些想要 GIL 的人提供了一个厚颜无耻的信息

IronPython uses the .NET framework and is written in C#. Compatibility is reaching the stage where Django can run on IronPython(at least as a demo) and there are guides to using threads in IronPython.

IronPython 使用 .NET 框架并用 C# 编写。兼容性达到了Django 可以在 IronPython 上运行的阶段(至少作为演示),并且有在 IronPython 中使用线程的指南

回答by Daren Thomas

The GIL (Global Interpreter Lock) might be a problem, but the API is quite OK. Try out the excellent processingmodule, which implements the Threading API for separate processes. I am using that right now (albeit on OS X, have yet to do some testing on Windows) and am really impressed. The Queue class is really saving my bacon in terms of managing complexity!

GIL(全局解释器锁)可能有问题,但 API 非常好。试试优秀的processing模块,它为单独的进程实现了线程 API。我现在正在使用它(尽管在 OS X 上,还没有在 Windows 上做一些测试)并且印象非常深刻。Queue 类在管理复杂性方面确实节省了我的培根!

EDIT: it seemes the processing module is being included in the standard library as of version 2.6 (import multiprocessing). Joy!

编辑:从版本 2.6 ( import multiprocessing)开始,处理模块似乎已包含在标准库中。喜悦!

回答by Henrik Gustafsson

As far as I know there are no real bugs, but the performance when threading in cPython is really bad (compared to most other threading implementations, but usually good enough if all most of the threads do is block) due to the GIL(Global Interpreter Lock), so really it is implementation specific rather than language specific. Jython, for example, does not suffer from this due to using the Java thread model.

据我所知没有真正的错误,但是由于GIL(全局解释器Lock),因此它实际上是特定于实现的,而不是特定于语言的。例如,Jython 不会因为使用 Java 线程模型而受到影响。

See thispost on why it is not really feasible to remove the GIL from the cPython implementation, and thisfor some practical elaboration and workarounds.

为什么它是不是真的可行,从CPython的实现去除GIL职位,对一些实际的阐述和解决方法。

Do a quick google for "Python GIL"for more information.

快速搜索“Python GIL”以获取更多信息。

回答by m-sharp

If you want to code in python and get great threading support, you might want to check out IronPython or Jython. Since the python code in IronPython and Jython run on the .NET CLR and Java VM respectively, they enjoy the great threading support built into those libraries. In addition to that, IronPython doesn't have the GIL, an issue that prevents CPython threads from taking full advantage of multi-core architectures.

如果您想用 Python 编写代码并获得出色的线程支持,您可能需要查看 IronPython 或 Jython。由于 IronPython 和 Jython 中的 Python 代码分别在 .NET CLR 和 Java VM 上运行,因此它们享受这些库中内置的强大线程支持。除此之外,IronPython 没有 GIL,这是一个阻止 CPython 线程充分利用多核架构的问题。

回答by tslocum

I've used it in several applications and have never had nor heard of threading being anything other than 100% reliable, as long as you know its limits. You can't spawn 1000 threads at the same time and expect your program to run properly on Windows, however you can easily write a worker pool and just feed it 1000 operations, and keep everything nice and under control.

我已经在多个应用程序中使用过它,并且从来没有听说过线程除了 100% 可靠之外还有其他任何东西,只要您知道它的限制。您不能同时生成 1000 个线程并期望您的程序在 Windows 上正常运行,但是您可以轻松编写一个工作池并为其提供 1000 个操作,并保持一切正常并在控制之下。