python生成器/yield協(xié)程/gevent寫(xiě)簡(jiǎn)單的圖片下載器功能示例
本文實(shí)例講述了python生成器/yield協(xié)程/gevent寫(xiě)簡(jiǎn)單的圖片下載器功能。分享給大家供大家參考,具體如下:
1、生成器:
'''第二種生成器''' # 函數(shù)只有有yield存在就是生成器 def test(i): while True: i += 1 res = yield i print(res) i += 1 return res def main(): t = test(1) # 創(chuàng)建生成器對(duì)象 print(next(t)) # next第一次執(zhí)行從上到下,yield是終點(diǎn) print(next(t)) print(t.send(5)) if __name__ == '__main__': main()
運(yùn)行結(jié)果:
2
None
4
5
6
2、yield協(xié)程demo:
def run1(): while True: print('run1____') yield def run2(): while True: print('run2____') yield def main(): while True: next(run1()) next(run2()) if __name__ == '__main__': main()
3、gevent寫(xiě)簡(jiǎn)單的下載圖片
import gevent import requests,lxml # from gevent import monkey # monkey.patch_all() def get_pic(url, list): headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' } response = requests.get(url, headers=headers) with open('./pic/'+str(list.pop(0)) + '.png', 'wb') as f: f.write(response.content) def get_pic_name_list(): def main(): get_pic_name_list() list = [x for x in range(9999)] gevent.joinall([ gevent.spawn(get_pic, 'http://pic8.iqiyipic.com/image/20181008/eb/af/v_116880780_m_601_m11_180_236.jpg', list), gevent.spawn(get_pic, 'http://pic6.iqiyipic.com/image/20181004/a2/2b/v_112874372_m_601_m15_180_236.jpg', list) ]) if __name__ == '__main__': main()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python實(shí)現(xiàn)視頻讀取和轉(zhuǎn)化圖片
今天小編就為大家分享一篇python實(shí)現(xiàn)視頻讀取和轉(zhuǎn)化圖片,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python爬蟲(chóng)的兩套解析方法和四種爬蟲(chóng)實(shí)現(xiàn)過(guò)程
本文想針對(duì)某一網(wǎng)頁(yè)對(duì) python 基礎(chǔ)爬蟲(chóng)的兩大解析庫(kù)( BeautifulSoup 和 lxml )和幾種信息提取實(shí)現(xiàn)方法進(jìn)行分析,及同一網(wǎng)頁(yè)爬蟲(chóng)的四種實(shí)現(xiàn)方式,需要的朋友參考下吧2018-07-07python登錄QQ郵箱發(fā)信的實(shí)現(xiàn)代碼
python登錄QQ郵箱發(fā)信的代碼,有需要的朋友可以參考下2013-02-02利用python實(shí)現(xiàn)詞頻統(tǒng)計(jì)分析的代碼示例
詞頻統(tǒng)計(jì)是指在文本或語(yǔ)音數(shù)據(jù)中,統(tǒng)計(jì)每個(gè)單詞或符號(hào)出現(xiàn)的次數(shù),以便對(duì)文本或語(yǔ)音數(shù)據(jù)進(jìn),這篇文章將詳細(xì)介紹分詞后如何進(jìn)行詞頻統(tǒng)計(jì)分析2023-06-06python 實(shí)現(xiàn)rolling和apply函數(shù)的向下取值操作
這篇文章主要介紹了python 實(shí)現(xiàn)rolling和apply函數(shù)的向下取值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06python實(shí)現(xiàn)代碼統(tǒng)計(jì)程序
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)代碼統(tǒng)計(jì)程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09