有关python GIL的问题 [英] A question on python GIL

查看:53
本文介绍了有关python GIL的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

Python的说唱效果比应有的差一些.在三种(实际上是2.5种)案例中,它们实际上可以为您带来好处:

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

  • 如果非Python代码(例如C库,内核等)正在运行,则其他Python线程可以继续执行.这是纯Python代码,无法一次在两个线程中运行.因此,如果您正在执行磁盘或网络I/O,则线程确实可以为您买到东西,因为大部分时间都花在了Python本身之外.

  • 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.

GIL实际上不是 Python 的一部分,它是 CPython 的实现细节(Python核心开发人员从事的参考"实现,并且如果您仅在Linux机器上运行"python"之类的程序,就会得到通常的结果.

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和其他任何重新实现的Python通常没有 有GIL,并且多个纯Python线程可以同时执行.

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

0.5的情况:即使您完全是纯Python的用户,也几乎没有从线程中获得性能收益,但就开发人员的时间和解决线程的难度而言,有些问题确实很方便.当然,这也部分取决于开发人员.

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.

这篇关于有关python GIL的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆