Python线程性能与核心数量

时间:2016-05-23 07:26:35

标签: python python-multithreading gil

我是python的新手,我正在学习线程和GIL。 这些是@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,600,300); @charset "UTF-8"; /* Base Styles */ #cssmenu, #cssmenu ul, #cssmenu li, #cssmenu a { margin: 0; padding: 0; border: 0; list-style: none; font-weight: normal; direction: rtl; line-height: 1.5; font-family: tahoma, "Helvetica Neue", "Lucida Grande", sans-serif; font-size: 14px; position: relative; } #cssmenu { width: 750px; background: #fff; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; padding: 3px; -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.6); -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.6); box-shadow: 0 0 5px rgba(0, 0, 0, 0.6); direction: rtl; } #cssmenu > ul > li { margin: 0 0 2px 0; } #cssmenu > ul > li:last-child { margin: 0; } #cssmenu > ul > li > a { font-size: 15px; display: block; color: #ffffff; text-shadow: 0 1px 1px #000; background: #565656; background: -moz-linear-gradient(#565656 0%, #323232 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #565656), color-stop(100%, #323232)); background: -webkit-linear-gradient(#565656 0%, #323232 100%); background: linear-gradient(#565656 0%, #323232 100%); border: 1px solid #000; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; } #cssmenu > ul > li > a > span { display: block; border: 1px solid #666666; padding: 6px 10px; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; font-weight: bold; } #cssmenu > ul > li > a:hover { text-decoration: none; } #cssmenu > ul > li.active { border-bottom: none; } #cssmenu > ul > li.active > a { background: #97be10; background: -moz-linear-gradient(#97be10 0%, #79980d 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #97be10), color-stop(100%, #79980d)); background: -webkit-linear-gradient(#97be10 0%, #79980d 100%); background: linear-gradient(#97be10 0%, #79980d 100%); color: #fff; text-shadow: 0 1px 1px #000; border: 1px solid #79980d; } #cssmenu > ul > li.active > a span { border: 1px solid #97be10; } #cssmenu > ul > li.has-sub > a span { background: url(images/icon_plus.png) 2% center no-repeat; } #cssmenu > ul > li.has-sub.active > a span { background: url(images/icon_minus.png) 2% center no-repeat; } /* Sub menu */ #cssmenu ul ul { padding: 5px 12px; display: none; } #cssmenu ul ul li { padding: 3px 0; } #cssmenu ul ul a { display: block; color: #595959; font-size: 13px; font-weight: bold; } #cssmenu ul ul a:hover { color: #79980d; } #borderimg1 { border: 10px solid red; padding: 15px; -webkit-border-image: url(border.png) 30 round; /* Safari 3.1-5 */ -o-border-image: url(border.png) 30 round; /* Opera 11-12.1 */ //border-image: url(border.png) 30 round; } .divstyyle { direction: rtl; text-align: justify; }命令的统计信息:

lscpu

当我运行这个简单的python线程示例时,我得到以下输出。

 Architecture:          x86_64
 CPU op-mode(s):        32-bit, 64-bit
 Byte Order:            Little Endian
 CPU(s):                4
 On-line CPU(s) list:   0-3
 Thread(s) per core:    2
 Core(s) per socket:    2
 Socket(s):             1
 NUMA node(s):          1
 Vendor ID:             GenuineIntel
 CPU family:            6
 Model:                 69
 Stepping:              1
 CPU MHz:               1700.062
 BogoMIPS:              4789.05
 Virtualization:        VT-x
 L1d cache:             32K
 L1i cache:             32K
 L2 cache:              256K
 L3 cache:              3072K
 NUMA node0 CPU(s):     0-3

但是当我禁用3个内核并重新运行代码时:

import time
import threading

def counter(n):
    for i in range(0,n):
        i = i+1
    return

t1 = threading.Thread(target=counter, args = (10000000,))
t2 = threading.Thread(target=counter, args = (10000000,))

t0 = time.clock()
t1.start()
t2.start()
t1.join()
t2.join()
t3 = time.clock()

print "Total time : %s"%str(t3-t0)

bash@bash:~/Desktop$ python threads.py
Total time : 2.115326

这些数字或多或少保持不变。为什么会这样?有人解释。提前谢谢......

1 个答案:

答案 0 :(得分:2)

您正在使用的线程库实际上并未同时使用多个内核进行计算。

尝试使用multiprocessing模块代替计算线程,您应该会看到更多“预期”结果。