我們都知道并發(fā)(不是并行)編程目前有四種方式,多進(jìn)程,多線程,異步,和協(xié)程。
多進(jìn)程編程在python中有類似C的os.fork,當(dāng)然還有更高層封裝的multiprocessing標(biāo)準(zhǔn)庫(kù),在之前寫過的python高可用程序設(shè)計(jì)方法中提供了類似nginx中master process和worker process間信號(hào)處理的方式,保證了業(yè)務(wù)進(jìn)程的退出可以被主進(jìn)程感知。
多線程編程python中有Thread和threading,在linux下所謂的線程,實(shí)際上是LWP輕量級(jí)進(jìn)程,其在內(nèi)核中具有和進(jìn)程相同的調(diào)度方式,有關(guān)LWP,COW(寫時(shí)拷貝),fork,vfork,clone等的資料較多,這里不再贅述。
異步在linux下主要有三種實(shí)現(xiàn)select,poll,epoll,關(guān)于異步不是本文的重點(diǎn)。
說協(xié)程肯定要說yield,我們先來看一個(gè)例子:
#coding=utf-8import timeimport sys# 生產(chǎn)者def produce(l): i=0 while 1: if i < 5: l.append(i) yield i i=i+1 time.sleep(1) else: return # 消費(fèi)者def consume(l): p = produce(l) while 1: try: p.next() while len(l) > 0: print l.pop() except StopIteration: sys.exit(0)l = []consume(l)
在上面的例子中,當(dāng)程序執(zhí)行到produce的yield i時(shí),返回了一個(gè)generator,當(dāng)我們?cè)赾ustom中調(diào)用p.next(),程序又返回到produce的yield i繼續(xù)執(zhí)行,這樣l中又append了元素,然后我們print l.pop(),直到p.next()引發(fā)了StopIteration異常。
通過上面的例子我們看到協(xié)程的調(diào)度對(duì)于內(nèi)核來說是不可見的,協(xié)程間是協(xié)同調(diào)度的,這使得并發(fā)量在上萬的時(shí)候,協(xié)程的性能是遠(yuǎn)高于線程的。
import stacklessimport urllib2def output(): while 1: url=chan.receive() print url f=urllib2.urlopen(url) #print f.read() print stackless.getcurrent() def input(): f=open('url.txt') l=f.readlines() for i in l: chan.send(i)chan=stackless.channel()[stackless.tasklet(output)() for i in xrange(10)]stackless.tasklet(input)()stackless.run()
關(guān)于協(xié)程,可以參考greenlet,stackless,gevent,eventlet等的實(shí)現(xiàn)。
新聞熱點(diǎn)
疑難解答
圖片精選