我們都知道并發(不是并行)編程目前有四種方式,多進程,多線程,異步,和協程。
多進程編程在python中有類似C的os.fork,當然還有更高層封裝的multiprocessing標準庫,在之前寫過的python高可用程序設計方法中提供了類似nginx中master process和worker process間信號處理的方式,保證了業務進程的退出可以被主進程感知。
多線程編程python中有Thread和threading,在linux下所謂的線程,實際上是LWP輕量級進程,其在內核中具有和進程相同的調度方式,有關LWP,COW(寫時拷貝),fork,vfork,clone等的資料較多,這里不再贅述。
異步在linux下主要有三種實現select,poll,epoll,關于異步不是本文的重點。
說協程肯定要說yield,我們先來看一個例子:
#coding=utf-8 import time import sys # 生產者 def produce(l): i=0 while 1: if i < 5: l.append(i) yield i i=i+1 time.sleep(1) else: return # 消費者 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)
在上面的例子中,當程序執行到produce的yield i時,返回了一個generator,當我們在custom中調用p.next(),程序又返回到produce的yield i繼續執行,這樣l中又append了元素,然后我們print l.pop(),直到p.next()引發了StopIteration異常。
通過上面的例子我們看到協程的調度對于內核來說是不可見的,協程間是協同調度的,這使得并發量在上萬的時候,協程的性能是遠高于線程的。
import stackless import urllib2 def 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()
關于協程,可以參考greenlet,stackless,gevent,eventlet等的實現。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
