使用Python的Twisted框架編寫非阻塞程序的代碼示例
先來(lái)看一段代碼:
# ~*~ Twisted - A Python tale ~*~ from time import sleep # Hello, I'm a developer and I mainly setup Wordpress. def install_wordpress(customer): # Our hosting company Threads Ltd. is bad. I start installation and... print "Start installation for", customer # ...then wait till the installation finishes successfully. It is # boring and I'm spending most of my time waiting while consuming # resources (memory and some CPU cycles). It's because the process # is *blocking*. sleep(3) print "All done for", customer # I do this all day long for our customers def developer_day(customers): for customer in customers: install_wordpress(customer) developer_day(["Bill", "Elon", "Steve", "Mark"])
運(yùn)行一下,結(jié)果如下所示:
$ ./deferreds.py 1
------ Running example 1 ------ Start installation for Bill All done for Bill Start installation ... * Elapsed time: 12.03 seconds
這是一段順序執(zhí)行的代碼。四個(gè)消費(fèi)者,為一個(gè)人安裝需要3秒的時(shí)間,那么四個(gè)人就是12秒。這樣處理不是很令人滿意,所以看一下第二個(gè)使用了線程的例子:
import threading # The company grew. We now have many customers and I can't handle the # workload. We are now 5 developers doing exactly the same thing. def developers_day(customers): # But we now have to synchronize... a.k.a. bureaucracy lock = threading.Lock() # def dev_day(id): print "Goodmorning from developer", id # Yuck - I hate locks... lock.acquire() while customers: customer = customers.pop(0) lock.release() # My Python is less readable install_wordpress(customer) lock.acquire() lock.release() print "Bye from developer", id # We go to work in the morning devs = [threading.Thread(target=dev_day, args=(i,)) for i in range(5)] [dev.start() for dev in devs] # We leave for the evening [dev.join() for dev in devs] # We now get more done in the same time but our dev process got more # complex. As we grew we spend more time managing queues than doing dev # work. We even had occasional deadlocks when processes got extremely # complex. The fact is that we are still mostly pressing buttons and # waiting but now we also spend some time in meetings. developers_day(["Customer %d" % i for i in xrange(15)])
運(yùn)行一下:
$ ./deferreds.py 2
------ Running example 2 ------ Goodmorning from developer 0Goodmorning from developer 1Start installation forGoodmorning from developer 2 Goodmorning from developer 3Customer 0 ... from developerCustomer 13 3Bye from developer 2 * Elapsed time: 9.02 seconds
這次是一段并行執(zhí)行的代碼,使用了5個(gè)工作線程。15個(gè)消費(fèi)者每個(gè)花費(fèi)3s意味著總共45s的時(shí)間,不過(guò)用了5個(gè)線程并行執(zhí)行總共只花費(fèi)了9s的時(shí)間。這段代碼有點(diǎn)復(fù)雜,很大一部分代碼是用于管理并發(fā),而不是專注于算法或者業(yè)務(wù)邏輯。另外,程序的輸出結(jié)果看起來(lái)也很混雜,可讀性也天津市。即使是簡(jiǎn)單的多線程的代碼同樣也難以寫得很好,所以我們轉(zhuǎn)為使用Twisted:
# For years we thought this was all there was... We kept hiring more # developers, more managers and buying servers. We were trying harder # optimising processes and fire-fighting while getting mediocre # performance in return. Till luckily one day our hosting # company decided to increase their fees and we decided to # switch to Twisted Ltd.! from twisted.internet import reactor from twisted.internet import defer from twisted.internet import task # Twisted has a slightly different approach def schedule_install(customer): # They are calling us back when a Wordpress installation completes. # They connected the caller recognition system with our CRM and # we know exactly what a call is about and what has to be done next. # # We now design processes of what has to happen on certain events. def schedule_install_wordpress(): def on_done(): print "Callback: Finished installation for", customer print "Scheduling: Installation for", customer return task.deferLater(reactor, 3, on_done) # def all_done(_): print "All done for", customer # # For each customer, we schedule these processes on the CRM # and that # is all our chief-Twisted developer has to do d = schedule_install_wordpress() d.addCallback(all_done) # return d # Yes, we don't need many developers anymore or any synchronization. # ~~ Super-powered Twisted developer ~~ def twisted_developer_day(customers): print "Goodmorning from Twisted developer" # # Here's what has to be done today work = [schedule_install(customer) for customer in customers] # Turn off the lights when done join = defer.DeferredList(work) join.addCallback(lambda _: reactor.stop()) # print "Bye from Twisted developer!" # Even his day is particularly short! twisted_developer_day(["Customer %d" % i for i in xrange(15)]) # Reactor, our secretary uses the CRM and follows-up on events! reactor.run()
運(yùn)行結(jié)果:
------ Running example 3 ------ Goodmorning from Twisted developer Scheduling: Installation for Customer 0 .... Scheduling: Installation for Customer 14 Bye from Twisted developer! Callback: Finished installation for Customer 0 All done for Customer 0 Callback: Finished installation for Customer 1 All done for Customer 1 ... All done for Customer 14 * Elapsed time: 3.18 seconds
這次我們得到了完美的執(zhí)行代碼和可讀性強(qiáng)的輸出結(jié)果,并且沒(méi)有使用線程。我們并行地處理了15個(gè)消費(fèi)者,也就是說(shuō),本來(lái)需要45s的執(zhí)行時(shí)間在3s之內(nèi)就已經(jīng)完成。這個(gè)竅門就是我們把所有的阻塞的對(duì)sleep()的調(diào)用都換成了Twisted中對(duì)等的task.deferLater()和回調(diào)函數(shù)。由于現(xiàn)在處理的操作在其他地方進(jìn)行,我們就可以毫不費(fèi)力地同時(shí)服務(wù)于15個(gè)消費(fèi)者。
前面提到處理的操作發(fā)生在其他的某個(gè)地方?,F(xiàn)在來(lái)解釋一下,算術(shù)運(yùn)算仍然發(fā)生在CPU內(nèi),但是現(xiàn)在的CPU處理速度相比磁盤和網(wǎng)絡(luò)操作來(lái)說(shuō)非???。所以給CPU提供數(shù)據(jù)或者從CPU向內(nèi)存或另一個(gè)CPU發(fā)送數(shù)據(jù)花費(fèi)了大多數(shù)時(shí)間。我們使用了非阻塞的操作節(jié)省了這方面的時(shí)間,例如,task.deferLater()使用了回調(diào)函數(shù),當(dāng)數(shù)據(jù)已經(jīng)傳輸完成的時(shí)候會(huì)被激活。
另一個(gè)很重要的一點(diǎn)是輸出中的Goodmorning from Twisted developer和Bye from Twisted developer!信息。在代碼開(kāi)始執(zhí)行時(shí)就已經(jīng)打印出了這兩條信息。如果代碼如此早地執(zhí)行到了這個(gè)地方,那么我們的應(yīng)用真正開(kāi)始運(yùn)行是在什么時(shí)候呢?答案是,對(duì)于一個(gè)Twisted應(yīng)用(包括Scrapy)來(lái)說(shuō)是在reactor.run()里運(yùn)行的。在調(diào)用這個(gè)方法之前,必須把應(yīng)用中可能用到的每個(gè)Deferred鏈準(zhǔn)備就緒,然后reactor.run()方法會(huì)監(jiān)視并激活回調(diào)函數(shù)。
注意,reactor的主要一條規(guī)則就是,你可以執(zhí)行任何操作,只要它足夠快并且是非阻塞的。
現(xiàn)在好了,代碼中沒(méi)有那么用于管理多線程的部分了,不過(guò)這些回調(diào)函數(shù)看起來(lái)還是有些雜亂??梢孕薷某蛇@樣:
# Twisted gave us utilities that make our code way more readable! @defer.inlineCallbacks def inline_install(customer): print "Scheduling: Installation for", customer yield task.deferLater(reactor, 3, lambda: None) print "Callback: Finished installation for", customer print "All done for", customer def twisted_developer_day(customers): ... same as previously but using inline_install() instead of schedule_install() twisted_developer_day(["Customer %d" % i for i in xrange(15)]) reactor.run()
運(yùn)行的結(jié)果和前一個(gè)例子相同。這段代碼的作用和上一個(gè)例子是一樣的,但是看起來(lái)更加簡(jiǎn)潔明了。inlineCallbacks生成器可以使用一些一些Python的機(jī)制來(lái)使得inline_install()函數(shù)暫?;蛘呋謴?fù)執(zhí)行。inline_install()函數(shù)變成了一個(gè)Deferred對(duì)象并且并行地為每個(gè)消費(fèi)者運(yùn)行。每次yield的時(shí)候,運(yùn)行就會(huì)中止在當(dāng)前的inline_install()實(shí)例上,直到y(tǒng)ield的Deferred對(duì)象完成后再恢復(fù)運(yùn)行。
現(xiàn)在唯一的問(wèn)題是,如果我們不止有15個(gè)消費(fèi)者,而是有,比如10000個(gè)消費(fèi)者時(shí)又該怎樣?這段代碼會(huì)同時(shí)開(kāi)始10000個(gè)同時(shí)執(zhí)行的序列(比如HTTP請(qǐng)求、數(shù)據(jù)庫(kù)的寫操作等等)。這樣做可能沒(méi)什么問(wèn)題,但也可能會(huì)產(chǎn)生各種失敗。在有巨大并發(fā)請(qǐng)求的應(yīng)用中,例如Scrapy,我們經(jīng)常需要把并發(fā)的數(shù)量限制到一個(gè)可以接受的程度上。在下面的一個(gè)例子中,我們使用task.Cooperator()來(lái)完成這樣的功能。Scrapy在它的Item Pipeline中也使用了相同的機(jī)制來(lái)限制并發(fā)的數(shù)目(即CONCURRENT_ITEMS設(shè)置):
@defer.inlineCallbacks def inline_install(customer): ... same as above # The new "problem" is that we have to manage all this concurrency to # avoid causing problems to others, but this is a nice problem to have. def twisted_developer_day(customers): print "Goodmorning from Twisted developer" work = (inline_install(customer) for customer in customers) # # We use the Cooperator mechanism to make the secretary not # service more than 5 customers simultaneously. coop = task.Cooperator() join = defer.DeferredList([coop.coiterate(work) for i in xrange(5)]) # join.addCallback(lambda _: reactor.stop()) print "Bye from Twisted developer!" twisted_developer_day(["Customer %d" % i for i in xrange(15)]) reactor.run() # We are now more lean than ever, our customers happy, our hosting # bills ridiculously low and our performance stellar. # ~*~ THE END ~*~
運(yùn)行結(jié)果:
$ ./deferreds.py 5 ------ Running example 5 ------ Goodmorning from Twisted developer Bye from Twisted developer! Scheduling: Installation for Customer 0 ... Callback: Finished installation for Customer 4 All done for Customer 4 Scheduling: Installation for Customer 5 ... Callback: Finished installation for Customer 14 All done for Customer 14 * Elapsed time: 9.19 seconds
從上面的輸出中可以看到,程序運(yùn)行時(shí)好像有5個(gè)處理消費(fèi)者的槽。除非一個(gè)槽空出來(lái),否則不會(huì)開(kāi)始處理下一個(gè)消費(fèi)者的請(qǐng)求。在本例中,處理時(shí)間都是3秒,所以看起來(lái)像是5個(gè)一批次地處理一樣。最后得到的性能跟使用線程是一樣的,但是這次只有一個(gè)線程,代碼也更加簡(jiǎn)潔更容易寫出正確的代碼。
PS:deferToThread使同步函數(shù)實(shí)現(xiàn)非阻塞
wisted的defer.Deferred (from twisted.internet import defer)可以返回一個(gè)deferred對(duì)象.
注:deferToThread使用線程實(shí)現(xiàn)的,不推薦過(guò)多使用
***把同步函數(shù)變?yōu)楫惒?返回一個(gè)Deferred)***
twisted的deferToThread(from twisted.internet.threads import deferToThread)也返回一個(gè)deferred對(duì)象,不過(guò)回調(diào)函數(shù)在另一個(gè)線程處理,主要用于數(shù)據(jù)庫(kù)/文件讀取操作
.. # 代碼片段 def dataReceived(self, data): now = int(time.time()) for ftype, data in self.fpcodec.feed(data): if ftype == 'oob': self.msg('OOB:', repr(data)) elif ftype == 0x81: # 對(duì)服務(wù)器請(qǐng)求的心跳應(yīng)答(這個(gè)是解析 防疲勞駕駛儀,發(fā)給gps上位機(jī)的,然后上位機(jī)發(fā)給服務(wù)器的) self.msg('FP.PONG:', repr(data)) else: self.msg('TODO:', (ftype, data)) d = deferToThread(self.redis.zadd, "beier:fpstat:fps", now, self.devid) d.addCallback(self._doResult, extra)
下面這兒完整的例子可以給大家參考一下
# -*- coding: utf-8 -*- from twisted.internet import defer, reactor from twisted.internet.threads import deferToThread import functools import time # 耗時(shí)操作 這是一個(gè)同步阻塞函數(shù) def mySleep(timeout): time.sleep(timeout) # 返回值相當(dāng)于加進(jìn)了callback里 return 3 def say(result): print "耗時(shí)操作結(jié)束了, 并把它返回的結(jié)果給我了", result # 用functools.partial包裝一下, 傳遞參數(shù)進(jìn)去 cb = functools.partial(mySleep, 3) d = deferToThread(cb) d.addCallback(say) print "你還沒(méi)有結(jié)束我就執(zhí)行了, 哈哈" reactor.run()
- python如何通過(guò)twisted搭建socket服務(wù)
- Python3.6中Twisted模塊安裝的問(wèn)題與解決
- python安裝twisted的問(wèn)題解析
- python如何通過(guò)twisted實(shí)現(xiàn)數(shù)據(jù)庫(kù)異步插入
- python基于twisted框架編寫簡(jiǎn)單聊天室
- python 編程之twisted詳解及簡(jiǎn)單實(shí)例
- Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼
- 剖析Python的Twisted框架的核心特性
- 實(shí)例解析Python的Twisted框架中Deferred對(duì)象的用法
- 詳解Python的Twisted框架中reactor事件管理器的用法
- Python的Twisted框架中使用Deferred對(duì)象來(lái)管理回調(diào)函數(shù)
- 使用Python的Twisted框架構(gòu)建非阻塞下載程序的實(shí)例教程
- Python的Twisted框架上手前所必須了解的異步編程思想
- 使用Python的Treq on Twisted來(lái)進(jìn)行HTTP壓力測(cè)試
- 利用Python的Twisted框架實(shí)現(xiàn)webshell密碼掃描器的教程
- 使用Python的Twisted框架實(shí)現(xiàn)一個(gè)簡(jiǎn)單的服務(wù)器
- 使用Python的Twisted框架編寫簡(jiǎn)單的網(wǎng)絡(luò)客戶端
- python開(kāi)發(fā)實(shí)例之Python的Twisted框架中Deferred對(duì)象的詳細(xì)用法與實(shí)例
相關(guān)文章
Python中用字符串調(diào)用函數(shù)或方法示例代碼
字符串作為python中常用的數(shù)據(jù)類型,掌握字符串的常用方法十分必要。下面這篇文章主要給大家介紹了關(guān)于Python中通過(guò)字符串調(diào)用函數(shù)或方法的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08基于Python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
這篇文章主要為大家詳細(xì)介紹了Python如何通過(guò)WechatPCAPI來(lái)實(shí)現(xiàn)微信自動(dòng)回復(fù)的功能,文中的示例代碼講解詳細(xì),快跟隨小編一起動(dòng)手嘗試一下2022-06-06pytorch 中pad函數(shù)toch.nn.functional.pad()的用法
今天小編就為大家分享一篇pytorch 中pad函數(shù)toch.nn.functional.pad()的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python深度學(xué)習(xí)之Unet?語(yǔ)義分割模型(Keras)
這篇文章主要介紹了語(yǔ)義分割任務(wù)中Unet一個(gè)有意思的模型-Keras。Keras是一個(gè)由Python編寫的開(kāi)源人工神經(jīng)網(wǎng)絡(luò)庫(kù),可進(jìn)行深度學(xué)習(xí)模型的設(shè)計(jì)、調(diào)試、評(píng)估、應(yīng)用和可視化。感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)一下吧2021-12-12Python基于network模塊制作電影人物關(guān)系圖
這篇文章主要介紹了Python基于network模塊制作電影人物關(guān)系圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06