目前已經知道,在需要並發執行任務的時候,需要使用多線程或者多進程;如果是IO密集型任務,使用多線程,如果是CPU密集型任務,使用多進程;但問題是,經常我們會遇到一種情況就是:需要被執行的任務既有IO操作,又有計算操作,那么這種情況下,已經無法 直觀的判斷任務是IO操作的多還是計算操作的多了;
所以,在開始並發任務之前,可以先進行測試,看看是使用多線程還是多進程所用的時間少,那個少就用那個
python 多進程模塊multiprocessing,提供了多進程的進程池和多線程的線程池,輔助我們進行測試,如下:
from multiprocessing import Pool from multiprocessing.dummy import Pool as ThreadPool
其中第一個pool是多進程的進程池,第二個是線程池,如果查看dummy的源碼,可以看到dummy繼承自Threading.thread
class DummyProcess(threading.Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
threading.Thread.__init__(self, group, target, name, args, kwargs)
self._pid = None
self._children = weakref.WeakKeyDictionary()
self._start_called = False
self._parent = current_process()
multiprocessing.dummy實際上調用的是多線程的模塊,是對多線程模塊的進一步封裝,使得其和多進程的具有相同的API;
介紹完了模塊,我們使用實際的例子來測試任務:
任務:使用urllib請求多個url,並計算返回的字符串的長度;
分別使用多進程和多線程去執行該任務
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'Charles Chang'
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import freeze_support
import urllib2
urls = [
'http://www.python.org',
'http://www.python.org/about/',
'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
'http://www.python.org/doc/',
'http://www.python.org/download/',
'http://www.python.org/getit/',
'http://www.python.org/community/',
'https://wiki.python.org/moin/',
'http://planet.python.org/',
'https://wiki.python.org/moin/LocalUserGroups',
'http://www.python.org/psf/',
'http://docs.python.org/devguide/',
'http://www.python.org/community/awards/'
]
import time
def w1(func):
def inner(*args,**kwargs):
past = time.time()
func(*args,**kwargs)
now = time.time()
cost_time = now - past
print "The function <%s> cost time: <%s>"%(func.func_name,cost_time)
return inner
def test(n):
print len(urllib2.urlopen(n).read())
ppool = Pool(4)
@w1
def MulProcess():
for n in urls:
ppool.apply(func=test,args=(n,))
ppool.close()
ppool.join()
MulProcess()
tpool = ThreadPool(4)
@w1
def MulThreading():
for n in urls:
tpool.apply(func=test,args=(n,))
tpool.close()
tpool.join()
MulThreading()
運行結果:
[root@linux-node1 ~]# python m1.py 47436 40307 34778 38780 94856 94767 33406 22916 277026 108358 42671 66493 32669 The function <MulProcess> cost time: <55.7311470509> 47436 40307 34778 38780 94856 94767 33406 22916 277026 108358 42671 66493 32669 The function <MulThreading> cost time: <93.1050798893>
可以看到:使用多進程耗時短
參考鏈接:http://chriskiehl.com/article/parallelism-in-one-line/
