好吧 GIL ……
前段时间又玩了下,结果多线程还真把我的双核给用完了。
用到 python2.5.5、python2.6.4、python2.7、
unladen swallow q3、pypy-jit1.3 。当然,结果都一样。
见附件。首先是单线程,只用了一个核
(另外一个核正在用于娱乐,所以还有一点点负载那)。
然后是多线程,两个核这就满了。
…… 大概空循环不会碰到 GIL 吧 ^_^b
和赖神提了下,我哪里搞错了?
赖神也试了下,在他那里仍然只用了一个核。OK ……
CPU: AMD Athlon(tm) Dual Core Processor 5000B
(也就是 AMD 2.6GHz x 2)
面壁中。
测试脚本? 大家本地都测试一下?
- OS
- CPU
等等都不同时,可能有冼具...
> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
> 详情: https://groups.google.com/group/python-cn
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>
--
http://zoomquiet.org 人生苦短? Pythonic!
# CPU: AMD Athlon(tm) Dual Core Processor 5000B
from thread import start_new_thread
def loop():
while 1:
pass
start_new_thread(loop, ())
start_new_thread(loop, ())
start_new_thread(loop, ())
loop()
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
这个实验很久以前做过,起几个线程,每个线程里面做空循环。
然后看能把多核用完吗。结果只有一个 cpu 100% 了。
好吧 GIL ……
前段时间又玩了下,结果多线程还真把我的双核给用完了。
用到 python2.5.5、python2.6.4、python2.7、
、。当然,结果都一样。
见附件。首先是单线程,只用了一个核
(另外一个核正在用于娱乐,所以还有一点点负载那)。
然后是多线程,两个核这就满了。
…… 大概空循环不会碰到 GIL 吧 ^_^b
和赖神提了下,我哪里搞错了?
赖神也试了下,在他那里仍然只用了一个核。OK ……
CPU: AMD Athlon(tm) Dual Core Processor 5000B
(也就是 AMD 2.6GHz x 2)
面壁中。
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>
> from thread import start_new_thread
> def loop():
> while 1:
> pass
> start_new_thread(loop, ())
> start_new_thread(loop, ())
> start_new_thread(loop, ())
> loop()
--
http://zoomquiet.org 人生苦短? Pythonic!
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
2010/7/20 Shell Xu <shell...@gmail.com>:
--
http://zoomquiet.org 人生苦短? Pythonic!
看看我的测试结果:
环境: Ubuntu 10.04
CPU : i5 430M 2.26Hz, 1066 MHz FSB
$ python --version
Python 2.6.5
$ cat myapp.py
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
class MyThread(QtCore.QThread):
def __init__(self,name,parent = None):
super(QtCore.QThread,self).__init__(parent)
self.name = name
def run(self):
while 1:
print self.name
app = QtGui.QApplication(sys.argv)
t1 = MyThread("*** thread 1")
t2 = MyThread(" ** thread 2")
t3 = MyThread(" * thread 3")
t1.start()
t2.start()
t3.start()
sys.exit(app.exec_())
运行结果在附件。 我是双核丝线程CPU,可见CPU确实开了3个线程。
为了区别于大家的测试,我用了PyQt的线程封装。
Thanks
Kermit
补充一下,我注意到了一个细节:
如果我开一个线程,如t1.start(),t2,t3不开的话,有一个CPU会占用到100%,其
他几个CPU不动,这个情况正常。
但是大家再回头看我前面的测试结果,虽然3个CPU线程跑起来了,但是很明显,都
没有占满CPU资源。 这个有何解释?
Regards
Kermit
Kermit
请问一下,ZQ那个测试工具在命令行里看CPU的是什么东东? top的某个命令吗?
Thanks
Regards
Kermit
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
ZQ那么明显是命令行的,下面半段是top的界面,我想知道怎么调出那个在命令行
里显示多CPU进度的。
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
<snip>
> 最后一张是htop
哦,谢谢大家!
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
2010/7/21 刘鑫 <marc...@gmail.com>:
from thread import start_new_thread
data = [12,]
def loop():
while 1:
data[0] = data[0] % 12
data[0] += 1
start_new_thread(loop, ())
start_new_thread(loop, ())
start_new_thread(loop, ())
loop()
2010/7/21 机械唯物主义 : linjunhalida <linjun...@gmail.com>:
2010/7/21 机械唯物主义 : linjunhalida <linjun...@gmail.com>:
> 改了一下,速度每个CPU都变成50%左右了。
>
> from thread import start_new_thread
> data = [12,]
> def loop():
> while 1:
> data[0] = data[0] % 12
> data[0] += 1
> start_new_thread(loop, ())
> start_new_thread(loop, ())
> start_new_thread(loop, ())
> loop()
>
>
> 2010/7/21 机械唯物主义 : linjunhalida <linjun...@gmail.com>:
>> 3个cpu都70%左右。
>>
>> 2010/7/21 刘鑫 <marc...@gmail.com>:
>>>
>>>
>>> 在 2010年7月21日 下午8:45,MC.Spring <here...@gmail.com>写道:
>>>>
>>>> 刚刚测试了一下:
>>>>
>>>> Microsoft Windows XP
>>>>
>>>> AMD Athlon(tm) 64 X2 Dual
>>>>
>>>> Core Processor 3600+
>>>>
>>>> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
>>>> (Intel)] on win32
>>>>
>>>> 结果:通过!
>>>
>>> 这事儿神了......你们加些逻辑进去?
>>>>
>>>> --
>>>> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
>>>> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
>>>> 详情: https://groups.google.com/group/python-cn
>>>> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>>>
>>>
>>>
>>> --
>>> "那么告诉我,路西法·晨星,问问你们自己,你们所有人......若是十万天使军不再梦想天堂,地狱又有什么伟力?"----尼尔·盖曼《睡魔》
>>> ......
>>>
>>> 劉鑫
>>> March.Liu
>>>
>>> --
>>> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
>>> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
>>> 详情: https://groups.google.com/group/python-cn
>>> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>>>
>>
>
#!/usr/bin/env python
from time import time, sleep
from thread import start_new_thread
def ipp():
i = 0
while 1:
if time() > t:
print '[i]'%i
break
i += 1
t = time() + 5
start_new_thread(ipp, ())
start_new_thread(ipp, ())
start_new_thread(ipp, ())
start_new_thread(ipp, ())
ipp()
sleep(1)
线程开得越多,能做的循环就越少,性能越差。
大家洗洗睡吧,我继续面壁 。。。
On Jul 22, 8:10 am, 机械唯物主义 : linjunhalida <linjunhal...@gmail.com>
wrote:
�� 2010-7-22 9:17, ���� �:
> д�˸��µIJ��ԣ��dz���ֻ���õ����ˡ�������ѭ���Ǹ�����
>
> #!/usr/bin/env python
> from time import time, sleep
> from thread import start_new_thread
> def ipp():
> i = 0
> while 1:
> if time() > t:
> print '[i]'%i
> break
> i += 1
>
> t = time() + 5
> start_new_thread(ipp, ())
> start_new_thread(ipp, ())
> start_new_thread(ipp, ())
> start_new_thread(ipp, ())
> ipp()
> sleep(1)
>
> �߳̿���Խ�࣬������ѭ����Խ�٣�����Խ�
>
> ���ϴϴ˯�ɣ��Ҽ������ ������
>
> On Jul 22, 8:10 am, ��еΨ������ : linjunhalida <linjunhal...@gmail.com>
> wrote:
>> ���ǿ��ҷ��ĵڶ������ݣ�
>> ��Ȼû����֤���һ��Ǿ��ã�GIL�����еģ�
>> CPU��߲�����Ϊ��������Ҫ���������飬�������������ĵط���
>> ����ͨ������Ĵ����������������淢��pdf��������ӣ���
>>
>> import os, timeit, threading
>>
>> def count(n):
>> while n>0:
>> n -= 1
>>
>> def serial(n):
>> count(n)
>> count(n)
>>
>> def parallel(n):
>> t1 = threading.Thread(target=count, args=(n,))
>> t1.start()
>> t2 = threading.Thread(target=count, args=(n,))
>> t2.start()
>> t1.join()
>> t2.join()
>>
>> print "serial: ", timeit.timeit(lambda:serial(10**8), number=1)
>> print "parallel: ", timeit.timeit(lambda:parallel(10**8), number=1)
>>
>> ���(windows+python2.6)��
>> python main.py
>> serial: 12.971452619
>> parallel: 25.7048534668
>>
>> �ڼ�CPUû�г�ߣ���������Ϊlinux��windows����thread��ʵ�ַ�ʽ��ͬ��
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
On Jul 22, 9:24 am, 刘鑫 <march....@gmail.com> wrote:
> IO这里会不会也有问题,每个线程开一个socket呢?
On Jul 22, 9:24 am, 刘鑫 <march....@gmail.com> wrote:
> IO这里会不会也有问题,每个线程开一个socket呢?
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
这个我觉得有多个硬盘不会改变这个test case。 因为这种频繁读写在同一时间只
能有一个硬盘在处理,这种频繁操作应该没有办法交给DMA来做。只是猜测,不确
信。
Regards
Kermit
"机械唯物主义 : linjunhalida" <linjunhal...@gmail.com> wrote:
> 改了一下,速度每个CPU都变成50%左右了。
>
> from thread import start_new_thread
> data = [12,]
> def loop():
> while 1:
> data[0] = data[0] % 12
> data[0] += 1
> start_new_thread(loop, ())
> start_new_thread(loop, ())
> start_new_thread(loop, ())
> loop()
>
>
> 2010/7/21 机械唯物主义 : linjunhalida <linjun...@gmail.com>:
> > 3个cpu都70%左右。
> >
> > 2010/7/21 刘鑫 <marc...@gmail.com>:
> >>
> >>
> >> 在 2010年7月21日 下午8:45,MC.Spring <here...@gmail.com>写道:
> >>>
> >>> 刚刚测试了一下:
> >>>
> >>> Microsoft Windows XP
> >>>
> >>> AMD Athlon(tm) 64 X2 Dual
> >>>
> >>> Core Processor 3600+
> >>>
> >>> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> >>> (Intel)] on win32
> >>>
> >>> 结果:通过!
> >>
> >> 这事儿神了......你们加些逻辑进去?
> >>>
> >>> --
> >>> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
> >>> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
> >>> 详情: https://groups.google.com/group/python-cn
> >>> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
> >>
> >>
> >>
> >> --
> >> "那么告诉我,路西法·晨星,问问你们自己,你们所有人......若是十万天使军不再梦想天堂,地狱又有什么伟力?"----尼尔·盖曼《睡魔》
> >> ......
> >>