Python GIL的一个实验
看了这篇介绍GIL的文章,写的真好,GIL = Global Interpreter Lock, 全局解释器锁,在解释器解释执行任何Python代码时,都需要先获得这把锁才行,也就是一个Python解释器在任意时候都只能跑一个线程,在我的Macbook Pro上跑个实验试试,这些是我本本的参数:
CPU: 2.8 GHz Intel Core i7
➜ Desktop sysctl -a | grep cpu
➜ ~ sysctl -a | grep cpu
hw.ncpu: 4 # 系统使用的cpu数目是4个
hw.activecpu: 4
hw.physicalcpu: 2 # 物理cpu数目是2个
hw.physicalcpu_max: 2
hw.logicalcpu: 4
hw.logicalcpu_max: 4
...
machdep.cpu.brand_string: Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
...
machdep.cpu.logical_per_package: 16
machdep.cpu.cores_per_package: 8 # 这个不太清楚是啥
...
machdep.cpu.core_count: 2 # 双核
machdep.cpu.thread_count: 4 # 可以跑4线程,即4个Python解释器都跑死循环就能把CPU占到100%
实验很简单了,就是写死循环占cpu,先开一个terminal,运行命令top,查看进程和cpu使用情况,然后不断地开terminal,进python,跑死循环:
➜ ~ python
Python 2.7.10 (default, Oct 17 2015, 01:15:29)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> while True: pass
...
开1个的时候, top命令显示的CPU usage在25%左右
2个,50%左右
3个,70%左右(好奇为啥没到75%)
4个,98%左右(最大的数看到了99.3%)
5个,98%左右
6个,98%左右
所以在我的机器上一个python解释器只能获得1/4的cpu资源,虐一下自己的机器挺爽的,听到风扇声一下子就起来了,哈哈~ 最后再安利一下Python大法好,写个死循环都只需要一行~
2016年8月04日 10:51
我试了一下,并不能占到四分之一, python3.5
2016年9月02日 01:49
我的是2.7, 系统也要占点资源吧,不能都分给Python吧@tt:
2021年9月30日 13:23
The good news is, there are all the ways in order to avoid skin soreness as there are many of irritants as part of your household. Naturally, you could just skip home cleaning by on your own altogether and rely in deep cleanup services to perform the soiled job in your case. Hiring these kind of cleaning firms, however, can please take a chunk through your savings, and will not be the most suitable option always. Luckily, you'll find better options around for people just like you. Some ones are further down.
2022年9月17日 12:48
Department of Education and Secondary Education Board has designed the AP SSC Civics Model Paper 2023 Pdf with answers for Telugu Medium, English Medium & Urdu Medium Students of the State Board. Every year there are a huge number of teaching staff and educational portals of the state have suggested the practice question bank with revision questions for both medium students of the board. AP 10th Civics Question Paper In civics, students learn to contribute to public processes and discussions of real issues. Students can also learn civic practices such as voting, volunteering, jury service, and joining with others to improve society. Civics enables students not only to study how others participate but also to practice participating and taking informed action themselves.