您的位置:首页 > 编程语言 > Python开发

Python multi-process VS multi-thread (多核CPU利用率)

2013-05-14 13:55 381 查看
现在用python开发服务器代码,因此简单对比了一下其multi-process和multi-thread的CPU利用率

对比图(top命令),结论:python(cpython)由于GIL的存在无法使用threading充分利用CPU资源,如果服务器为多核,请考虑使用multi-process提升性能

多进程( multi-process)



多线程(multi-thread)



源代码

多进程( multi-process)

import multiprocessing

def thread_func():
print "thread in"
while True:
pass

if __name__ == "__main__":
t1 = multiprocessing.Process(target = thread_func)
t1.start()

t2 = multiprocessing.Process(target = thread_func)
t2.start()

t1.join()
t2.join()


多线程(multi-thread)

from threading import  Thread

def thread_func():
print "thread in"
while True:
pass

if __name__ == "__main__":
t1 = Thread(target = thread_func)
t1.start()

t2 = Thread(target = thread_func)
t2.start()

t1.join()
t2.join()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: