这篇文章将为大家详细讲解有关Python 中怎么利用多线程搜索txt文件内容,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
import threading
import os
class Find(threading.Thread): #搜索数据的线程类
def __init__(self,datalist,startIndex,endIndex,searchstr,savefile): #datalist要搜索的内容列表,startIndex列表搜索范围的开始下标,searchstr要搜索的内容
threading.Thread.__init__(self)
self.datalist=datalist #要搜索的数据的内存地址
self.startIndex=startIndex #开始的索引
self.endIndex=endIndex #结束的索引
self.seachstr=searchstr #需要搜索的数据
self.savefile=savefile
def run(self):
self.findlist=[]
for i in range(self.startIndex,self.endIndex):
line=self.datalist[i].decode("gbk","ignore") #读取一行
if line.find(self.seachstr)!=-1:
print(self.getName(),line,end="") #搜索数据
self.findlist.append(line)
global mutex #多线程共享全局变量(全局锁)
with mutex: #获取锁(自动释放锁)
for line in self.findlist:
self.savefile.write(line.encode("gbk"))
mutex=threading.Lock() #创建一个锁
savefile=open("c:\\zhaodao.txt","wb") #搜索到的内容写入该文件
path = "C:\\data1.txt" #要搜索的文件
file = open(path, "rb")
datalist = file.readlines() # 全部读入内存
lines=len(datalist) #所有的行数
searchstr=input("输入要查询的数据")
N=10 #开启10个线程
threadlist=[] #线程列表
# 97 9 0-1000000 1000000-2000000 2000000-3000000
for i in range(0,N-1): #0,1,2,3,4,5,6,7,8 数据切割
mythd= Find(datalist,i*(lines//(N-1)) , (i+1)*(lines//(N-1)),searchstr,savefile) # //表示整除
mythd.start()
threadlist.append(mythd) #添加到线程列表
#97 = 97//10*10=90
mylastthd= Find(datalist,lines//(N-1)*(N-1),lines,searchstr,savefile) #最后的线程搜索剩下的尾数
mylastthd.start()
threadlist.append(mylastthd) #添加到线程列表
for thd in threadlist: #遍历线程列表
thd.join()
print("finish")
关于Python 中怎么利用多线程搜索txt文件内容就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。