Table of Contents
回复内容:
Home Backend Development Python Tutorial Python有GIL为什么还需要线程同步?

Python有GIL为什么还需要线程同步?

Jun 06, 2016 pm 04:24 PM
gil python sys

Python 有GIL保证相关对象的同时操作,那为什么还需要进行线程同步?是不是因为在虚拟机层面之上在多线程工作时还需要提供相关的锁机制、队列机制,这其中的原理是什么?有其他类似的例子吗? 还是说Python内部的GIL保证了一个变量的引用数量,但是当多线程使用同一资源时,例如sys.stdout,这个时候是需要线程同步来确保资源访问不冲突,GIL不提供类似资源的保护吗?

回复内容:

GIL也只是相当于古时候单核CPU通过不断的分配时间片来模拟多线程的方法而已,为什么那个时候写多线程也要用锁? 这在网上都说烂了. 不过我当初也思考了这个问题一会儿.
你自己想想看这个情景
线程AB同时操作list
list的[0]初始值为0

线程A 操作100次
list[0]+=1
线程B 操作100次
list[0]+=1

在线程A 对于 list[0]进行操作时
list[0]为0, 还没等线程A完成加一操作, 就被切换到线程B了
在线程B 眼里,
list[0]还是为0, 于是执行加一操作.
再切换回线程A, 继续未完成的加一操作

你发现了没!!! 线程AB各对list[0]进行了加一,预期结果是2 但结果还是1
Python的list不是完全线程安全的.

所以加个线程锁就完了.
话说为什么, 有了线程锁还需要GIL呢?
因为, 有了GIL, 提供并发就变得很容易. 解释器只要计算每个线程的运行时间就好了
时间一到, 将这个线程冻结, 内存管理很简单.

等等, 你还是没解释, 如果我已经给线程上了锁, 为什么还是要被GIL限制?
一向符合人类直觉的Python, 有个很反直觉的机制.
Py的变量a其实不是C系编译语言的变量.
Python维护着一个字典, 储存着a和对应数值的指针.

用某黑Python的大牛的话说, Python企图用字典装下世界..
如果变成真多线程
对于这个字典的维护将会很复杂.
多个线程真正同时操作一个字典, Python引以为傲的字典性能, 估计就没那么强了.
就是说, Python字典的性能强大,是建立在线程不安全的基础上.
而字典在Python中的位置又是如此重要, 一个缓慢的字典, 会严重拖慢Python的解释速度.

多线程操作多个独立字典. 那样还是要同步.
那为什么不采用多进程呢? 这就是社区主流的看法.
虽然理论上线程成本更低, 但是那样代码就改成面目全非了.. GIL 的作用是:对于一个解释器,只能有一个thread在执行bytecode。所以每时每刻只有一条bytecode在被执行一个thread。GIL保证了bytecode 这层面上是thread safe的。

但是如果你有个操作比如 x += 1,这个操作需要多个bytecodes操作,在执行这个操作的多条bytecodes期间的时候可能中途就换thread了,这样就出现了data races的情况了。

比如这小家伙就有很多条bytecodes:
<span class="x">>>> dis.dis(lambda x: x+1)</span>
<span class="x">  1           0 LOAD_FAST                0 (x)</span>
<span class="x">              3 LOAD_CONST               1 (1)</span>
<span class="x">              6 BINARY_ADD</span>
<span class="x">              7 RETURN_VALUE</span>
Copy after login
youtube.com/watch? gil控制的是字节码, 锁控制的是python代码,粒度不一样吧? 比如用锁控制的代码被编译成101条字节码 那么线程在执行这101条字节码的时候,cpu会被其他线程利用(python调度线程默认是100条字节码 调度一次) Python有GIL为什么还需要线程同步?
——《Python源码剖析》 GIL是全局解释器锁,GIL保证了在同一时间片下总有一个Python(CPython实现)线程在执行。所以即使是多进程,而是顺序执行的。这样多线程并发就变得没有意义。

  • 线程在GIL下是有执行的时间片的
    • 在时间片内线程如果没有成功对数据进行操作,那么等到下一个时间片时,数据已经被别的线程修改了,那么得到的数据就不是想要的数据了
  • 线程的同步和互斥解决的是线程间数据的访问正确性问题,而GIL是实现当前Python解释器下只有一个线程在执行。两个是不同的概念。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1240
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles