关于python GIL的一个问题

发布于 2024-08-14 09:52:49 字数 214 浏览 5 评论 0原文

python GIL 的存在是否意味着在 python 多线程中,相同操作与在单线程中重复操作没有太大区别?

例如,如果我需要上传两个文件,那么在两个线程中执行它们而不是依次上传它们有什么好处?

我以两种方式尝试了一次大的数学运算。但它们似乎需要几乎相同的时间才能完成。

这对我来说似乎还不清楚。有人可以帮我解决这个问题吗? 谢谢。

Does the presence of python GIL imply that in python multi threading the same operation is not so different from repeating it in a single thread?.

For example, If I need to upload two files, what is the advantage of doing them in two threads instead of uploading them one after another?.

I tried a big math operation in both ways. But they seem to take almost equal time to complete.

This seems to be unclear to me. Can someone help me on this?.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佞臣 2024-08-21 09:52:49

Python 的线程得到的评价比其应有的要差一些。在三种(好吧,2.5)情况下,它们实际上会给您带来好处:

  • 如果非 Python 代码(例如 C 库、内核等)正在运行,其他 Python 线程可以继续执行。它只是纯Python 代码,不能同时在两个线程中运行。因此,如果您正在执行磁盘或网络 I/O,线程确实可以为您带来一些东西,因为大部分时间都花费在 Python 本身之外。

  • GIL 实际上并不是 Python 的一部分,它是 CPython 的实现细节(核心 Python 开发人员所从事的“参考”实现,您通常会使用它)如果你只是在 Linux 机器上运行“python”或者其他东西,就会得到。

    Jython、IronPython 和任何其他 Python 重新实现通常没有有 GIL,并且多个纯 Python 线程可以同时执行。

  • 0.5 的情况:即使您完全是纯 Python 并且从线程中看到很少或没有性能优势,但就开发人员的时间和使用线程解决的难度而言,某些问题确实很方便,这在一定程度上取决于线程 当然,开发者也有责任。

Python's threads get a slightly worse rap than they deserve. There are three (well, 2.5) cases where they actually get you benefits:

  • If non-Python code (e.g. a C library, the kernel, etc.) is running, other Python threads can continue executing. It's only pure Python code that can't run in two threads at once. So if you're doing disk or network I/O, threads can indeed buy you something, as most of the time is spent outside of Python itself.

  • The GIL is not actually part of Python, it's an implementation detail of CPython (the "reference" implementation that the core Python devs work on, and that you usually get if you just run "python" on your Linux box or something.

    Jython, IronPython, and any other reimplementations of Python generally do not have a GIL, and multiple pure-Python threads can execute simultaneously.

  • The 0.5 case: Even if you're entirely pure-Python and see little or no performance benefit from threading, some problems are really convenient in terms of developer time and difficulty to solve with threads. This depends in part on the developer, too, of course.

回梦 2024-08-21 09:52:49

这实际上取决于您使用的库。 GIL 旨在防止 Python 对象及其内部数据结构同时被更改。如果您正在进行上传,则用于执行实际上传的库可能会在等待实际 HTTP 请求完成时释放 GIL(我假设标准库中的 HTTP 模块就是这种情况,但我没检查)。

附带说明一下,如果您确实想让事情并行运行,只需使用多个进程即可。它会为你省去很多麻烦,并且最终会得到更好的代码(更健壮、更可扩展,而且很可能结构更好)。

It really depends on the library you're using. The GIL is meant to prevent Python objects and its internal data structures to be changed at the same time. If you're doing an upload, the library you use to do the actual upload might release the GIL while it's waiting for the actual HTTP request to complete (I would assume that is the case with the HTTP modules in the standard library, but I didn't check).

As a side note, if you really want to have things running in parallel, just use multiple processes. It will save you a lot of trouble and you'll end up with better code (more robust, more scalable, and most probably better structured).

梦情居士 2024-08-21 09:52:49

它取决于正在执行的本机代码模块。本机模块可以释放 GIL,然后继续执行自己的操作,从而允许另一个线程锁定 GIL。 GIL 通常在 Python 和本机代码在 Python 对象上运行时保存。如果您想要更多详细信息,您可能需要阅读大量相关内容。 :)

看:
什么是全局解释器锁 (GIL)?线程状态和全局解释器锁

It depends on the native code module that's executing. Native modules can release the GIL and then go off and do their own thing allowing another thread to lock the GIL. The GIL is normally held while code, both python and native, are operating on python objects. If you want more detail you'll probably need to go and read quite a bit about it. :)

See:
What is a global interpreter lock (GIL)? and Thread State and the Global Interpreter Lock

稀香 2024-08-21 09:52:49

多线程是一个概念,其中两个以上的任务需要同时完成,例如,我在这个应用程序中有文字处理器,有 N 个并行任务必须工作。就像听键盘、格式化输入文本、将格式化文本发送到显示单元一样。在这种顺序处理的情况下,它非常耗时,并且一个任务必须等待下一个任务完成。所以我们把这些任务放在线程中,同时完成任务。三个线程始终启动并等待输入到达,然后获取该输入并同时生成输出。

因此,如果我们拥有多核和处理器,多线程运行速度会更快。但实际上,在单处理器上,线程会一个接一个地工作,但我们感觉它的执行速度更快。实际上,一次执行一条指令,一个处理器一次可以执行数十亿条指令。因此计算机会产生多任务或线程并行工作的错觉。这只是一个幻觉。

Multithreading is a concept where two are more tasks need be completed simultaneously, for example, I have word processor in this application there are N numbers of a parallel task have to work. Like listening to keyboard, formatting input text, sending a formatted text to display unit. In this context with sequential processing, it is time-consuming and one task has to wait till the next task completion. So we put these tasks in threads and simultaneously complete the task. Three threads are always up and waiting for the inputs to arrive, then take that input and produce the output simultaneously.

So multi-threading works faster if we have multi-core and processors. But in reality with single processors, threads will work one after the other, but we feel it's executing with greater speed, Actually, one instruction executes at a time and a processor can execute billions of instructions at a time. So the computer creates illusion that multi-task or thread working parallel. It just an illusion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文