新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python多線程處理大量文件數(shù)據(jù)
在python中,我們會遇到處理大量文件數(shù)據(jù)的時候。如果我們用for循環(huán)順序處理,處理文件數(shù)據(jù)效率額很低,這時我們就可以使用我們的多線程來處理多個超大的文件數(shù)據(jù)。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名與空間、雅安服務器托管、營銷軟件、網(wǎng)站建設、修武網(wǎng)站維護、網(wǎng)站推廣。
第一步:導入import threading 模塊
import threading
第二步:使用多線程可以同時打開并運行多個數(shù)據(jù)
#! /usr/bin/env python
#encoding=utf-8
import threading
import time
from Queue import Queue
def readFile():
file_object = open('/opt/dev/python/list.dat')
global queue
for line in file_object:
queue.put(line)
class Consumer(threading.Thread):
def run(self):
global queue
while queue.qsize() > 0:
msg = self.name + '消費了 '+queue.get()
print msg
time.sleep(0.01)
queue = Queue()
def main():
readFile()
for i in range(5):
c = Consumer()
c.start()
if __name__ == '__main__':
main()
以上就是用python多線程處理大量數(shù)據(jù)的演示代碼,如果有需要處理大量文件的小伙伴,可以嘗試使用多線程,會方便很多哦~
(推薦操作系統(tǒng):windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。)
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python多線程處理大量文件數(shù)據(jù)
文章源于:http://www.5511xx.com/article/djghpgi.html


咨詢
建站咨詢
