流暢的Python 第17章 使用期物處理并發(fā) 筆記

本章介紹了從Python3.2以來引入的concurrent.futures模塊,阻塞性I/O與GIL拇惋,以及期物的概念勋功。

這一章給的示例代碼中的網(wǎng)頁http://flupy.org/data/flags目前無法訪問,這里就不再復(fù)制無法執(zhí)行的示例代碼了米绕,反正在這里也可直接查看垢村。

作為看完后的應(yīng)用割疾,試著對一段實際代碼進行改進。
下面是一段通過訪問網(wǎng)易財經(jīng)的接口得到上證和深證股票歷史數(shù)據(jù)嘉栓,寫入csv文件的程序

# -*- coding: utf-8 -*-
import csv
import datetime

import requests
from lxml import etree

now = datetime.datetime.now()
today = now.strftime('%Y%m%d')
before = now + datetime.timedelta(-1)
yesterday = before.strftime('%Y%m%d')


def getHTMLtext(url, param={}, code='ANSI'):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
    try:
        r = requests.get(url, param, headers=headers)
        r.encoding = code
        r.raise_for_status()
        return r.text
    except:
        return ''


def stockList(url, param={}):
    stockSH, stockSZ = [], []
    text = getHTMLtext(url, param)
    html = etree.HTML(text)
    for url in html.xpath('//a/@href'):
        if 'http://quote.eastmoney.com/sh' in url:
                stockSH.append(url.split('sh')[1][:6])
        elif 'http://quote.eastmoney.com/sz' in url:
                stockSZ.append(url.split('sz')[1][:6])
    print('all stock number got!')
    return stockSH, stockSZ


def stockInfo(lst, strnum):
    # 依序下載宏榕,無并發(fā)
    print('start get all stock history, will take a long time, please wait...')
    L = []
    for i in lst:
        # code=0000001分開看,0是上證胸懈,1是深證担扑,000001是股票代碼
        history_url = "http://quotes.money.163.com/service/chddata.html?code={0}&start={1}&end={2}&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER".format(strnum+i, '20170801', yesterday)
        perday = getHTMLtext(history_url).split('\r\n')
        if len(perday) <= 2:
            continue
        perday.pop()
        for day in perday[1:]:
            L.append(day.split(','))
    print('all stock info got!')

    with open(today+'stock.csv', 'a+', newline='', encoding='gb18030') as file:
        w = csv.writer(file)
        w.writerows(L)

    return L


def main():
    stockList_url = "http://quote.eastmoney.com/stocklist.html"
    SH, SZ = stockList(stockList_url)
    stockInfo(SH, '0')
    stockInfo(SZ, '1')


if __name__ == "__main__":
    main()

由于是依序下載,速度比較慢趣钱,這段代碼要運行十分鐘以上才執(zhí)行完畢涌献。

那么,現(xiàn)在是利用本章內(nèi)容加速代碼運行的時候了首有。
很明顯燕垃,stockInfo函數(shù)中的for循環(huán)互相之間沒有聯(lián)系,可以改成函數(shù)以便并發(fā)調(diào)用井联。

# -*- coding: utf-8 -*-
import csv
import datetime
from concurrent import futures

import requests
from lxml import etree

MAX_WORKERS = 20
L = []

now = datetime.datetime.now()
today = now.strftime('%Y%m%d')
before = now + datetime.timedelta(-1)
yesterday = before.strftime('%Y%m%d')


def getHTMLtext(url, param={}, code='ANSI'):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
    try:
        r = requests.get(url, param, headers=headers)
        r.encoding = code
        r.raise_for_status()
        return r.text
    except:
        return ''


def stockList(url, param={}):
    stockSH, stockSZ = [], []
    text = getHTMLtext(url, param)
    html = etree.HTML(text)
    for url in html.xpath('//a/@href'):
        if 'http://quote.eastmoney.com/sh' in url:
                stockSH.append(url.split('sh')[1][:6])
        elif 'http://quote.eastmoney.com/sz' in url:
                stockSZ.append(url.split('sz')[1][:6])
    print('all stock number got!')
    return stockSH, stockSZ


def stockInfo(i, num: str):
    # code=0000001應(yīng)該分開看卜壕,0是上證,1是深證烙常,000001是股票代碼
    history_url = "http://quotes.money.163.com/service/chddata.html?code={0}&start={1}&end={2}&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER".format(num+i, '20170820', yesterday)
    perday = getHTMLtext(history_url).split('\r\n')
    if len(perday) > 2:
        perday.pop()
        for day in perday[1:]:
            L.append(day.split(','))
    return L


def write2CSV(lst: list):
    with open(today+'stock.csv', 'a+', newline='', encoding='gb18030') as file:
        w = csv.writer(file)
        w.writerows(lst)


def downloadOne(i):
    luca = stockInfo(i, '0')
    write2CSV(luca)
    # stockInfo(lst, '1') 沒有跑深證的股票


def downloadMany(lst: list):
    workers = min(MAX_WORKERS, len(lst))
    with futures.ThreadPoolExecutor(workers) as e:
        res = e.map(downloadOne, sorted(lst))

    return len(list(res))


def main():
    stockList_url = "http://quote.eastmoney.com/stocklist.html"
    SH, SZ = stockList(stockList_url)
    downloadMany(SH)
    # downloadMany(SZ)


if __name__ == "__main__":
    main()

由于開了workers個線程轴捎,速度近似提高了workers倍,明顯感覺到一下子就執(zhí)行完畢了蚕脏。

事實上侦副,很多時候Python的多線程是無法提速的。這是因為CPython解釋器本身不是線程安全的驼鞭,因此存在全局解釋器鎖(GIL, global interpreter lock)秦驯,1次只允許1個線程執(zhí)行Python代碼,因此挣棕,1個Python進程通常不能使用多個CPU核心译隘。與Python語言本身無關(guān)亲桥,Jython等沒有這個限制。

不過固耘,標(biāo)準(zhǔn)庫中所有執(zhí)行阻塞型I/O操作的函數(shù)在等待操作系統(tǒng)返回結(jié)果時都會釋放GIL题篷,這意味著I/O密集型Python程序在多線程下可以正常運轉(zhuǎn):1個Python線程等待網(wǎng)絡(luò)響應(yīng)時,阻塞型I/O函數(shù)會釋放GIL玻驻,從而再運行1個線程悼凑。
包括 time.sleep()函數(shù),即使sleep(0)璧瞬,也會釋放GIL。

那么渐夸,如果是CPU密集型Python程序嗤锉,無法使用多線程時,怎么辦墓塌?
這時可以使用多進程瘟忱。
使用concurrent.futures模塊啟動多進程非常簡單,只需要把

def downloadMany(lst: list):
    workers = min(MAX_WORKERS, len(lst))
    with futures.ThreadPoolExecutor(workers) as e:
        res = e.map(downloadOne, sorted(lst))

改成

def downloadMany(lst: list):
    with futures.ProcessPoolExecutor() as e:
        res = e.map(downloadOne, sorted(lst))

就可以了苫幢。
ThreadPoolExecutor()函數(shù)需要1個參數(shù)指定線程池中線程的數(shù)量访诱,而ProcessPoolExecutor()函數(shù)通常不需要指定進程數(shù),默認是os.cpu_count()函數(shù)返回的CPU數(shù)量韩肝,也可以自行指定其他值触菜,但對CPU密集型的處理而言,進程數(shù)不得超過CPU數(shù)量哀峻。
實際上涡相,對于I/O密集型程序,這個框架提供的多進程方式做不到進程間通信剩蟀,不會節(jié)省運行時間催蝗,只是同時在跑4個完全一樣的程序最終得到4組相同數(shù)據(jù)而已。要實現(xiàn)進程間通信應(yīng)該使用multiprocessing模塊育特。

譯者把future翻譯成期物丙号,future是一種對象,表示異步執(zhí)行的操作缰冤,通常自己不應(yīng)該創(chuàng)建它犬缨,而是由并發(fā)框架實例化。
要創(chuàng)建的話锋谐,可以這樣寫

def downloadMany(lst: list):
    workers = min(MAX_WORKERS, len(lst))
    with futures.ThreadPoolExecutor(workers) as e:
        to_do = []
        for i in sorted(lst):
            future = e.submit(downloadOne, i)
            to_do.append(future)

        results = []
        for future in futures.as_completed(to_do):
            res = future.result()
            results.append(res)

    return len(results)

其實這個函數(shù)的return并沒有多大作用遍尺,發(fā)生異常的話會在此拋出而已。

上面有一點沒有提到涮拗,如何向map函數(shù)傳入多個參數(shù)乾戏?不過網(wǎng)上有一堆教程迂苛,暫時就不寫了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鼓择,一起剝皮案震驚了整個濱河市三幻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌呐能,老刑警劉巖念搬,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異摆出,居然都是意外死亡朗徊,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門偎漫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爷恳,“玉大人,你說我怎么就攤上這事象踊∥虑祝” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我诫舅,道長所袁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘头镊。我一直安慰自己,他們只是感情好魄幕,可當(dāng)我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布相艇。 她就那樣靜靜地躺著,像睡著了一般纯陨。 火紅的嫁衣襯著肌膚如雪坛芽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天翼抠,我揣著相機與錄音咙轩,去河邊找鬼。 笑死阴颖,一個胖子當(dāng)著我的面吹牛活喊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播量愧,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼钾菊,長吁一口氣:“原來是場噩夢啊……” “哼帅矗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起煞烫,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤浑此,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后滞详,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凛俱,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年料饥,在試婚紗的時候發(fā)現(xiàn)自己被綠了蒲犬。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡岸啡,死狀恐怖暖哨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情凰狞,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布沛慢,位于F島的核電站赡若,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏团甲。R本人自食惡果不足惜逾冬,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望躺苦。 院中可真熱鬧身腻,春花似錦、人聲如沸匹厘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽愈诚。三九已至她按,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間炕柔,已是汗流浹背酌泰。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留匕累,地道東北人陵刹。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像欢嘿,于是被迫代替她去往敵國和親衰琐。 傳聞我的和親對象是個殘疾皇子也糊,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容