亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      python

      當(dāng)前位置:中華考試網(wǎng) >> python >> python編程基礎(chǔ) >> 文章內(nèi)容

      多個(gè)數(shù)據(jù)如何用python3中的多線程處理?

      來源:中華考試網(wǎng)  [2020年11月24日]  【

        當(dāng)我們好不容易獲取了一些數(shù)據(jù)時(shí),想要進(jìn)行處理的話,通常我們的python小白會(huì)選擇for循環(huán)語句,畢竟這是我們?nèi)腴T時(shí)候就接觸了的。當(dāng)然這種方法是可行的,對(duì)于沒有過多接觸python模塊的小伙伴們來說已經(jīng)非常棒了。不過,小編今天推薦大家使用我們最近學(xué)習(xí)的python爬蟲中的多線程進(jìn)行解決,在時(shí)間效率方面非常節(jié)約。

        第 一步:import threading 模塊

        import threading

        第二步:改下一下代碼:

        既可以同時(shí)打開并運(yùn)行多個(gè)文件

        import operator

        import csv

        import time

        import threading

        from time import ctime

      python課程免費(fèi)試聽預(yù)約

      • 地區(qū):
      • 姓名:
      • 手機(jī):

        def read_file(filpos,i):

        with open(filpos+str(i)+".csv") as f:

        reader=csv.reader(f)

        for i in reader:

        print(i)

        threads = []

        x=0

        for t in range(0,3):

        t= threading.Thread(target=read_file,args=("D:/zhihu/",x))

        threads.append(t)

        x+=1

        #join在里面時(shí)候只有第 一個(gè)子進(jìn)程結(jié)束才能打開第二個(gè)進(jìn)程,if__name__ 調(diào)用時(shí)不可用

        if __name__=="__main__":

        for thr in threads:

        thr.start()

        thr.join()

        print("all over %s"%ctime())

        就是把文件添加到線程池里面,再一起執(zhí)行。

        通過以上多線程的代碼,我們能比使用for循環(huán)節(jié)約大量的等待時(shí)間,小伙伴們還不心動(dòng)嗎?具體的好處還是需要小伙伴們自己動(dòng)手才能體會(huì)~

      責(zé)編:fushihao

      上一篇:如何使用pythonmatplotlib庫繪制扇形?

      下一篇: 沒有了

      • 會(huì)計(jì)考試
      • 建筑工程
      • 職業(yè)資格
      • 醫(yī)藥考試
      • 外語考試
      • 學(xué)歷考試