需求
- 有5个小文件,每个线程读取一行,然后写入指定文件。另一个文件接着读取接着写入
- 一轮执行完接着下一轮
文件格式:
- 第一个文件全部是1,第二文件全部是2,等等
结果
最后汇总一个总文件
文件按123451234512345…..(每个数字是一行)
import threading list=[] import time lock=threading.Lock() class go(threading.Thread): time.sleep(1) def __init__(self,index): threading.Thread.__init__(self) self.index=index def run(self): fh=open("/Users/new/lean/test/docter/线程作业/ip "+str(self.index)+".txt") for line in fh: while (list[0]!=self.index):#如果当前不该轮到这个线程,就不运行 pass op=open("/Users/new/lean/test/docter/线程作业/zuoye.txt",'a+') op.write(line) print(str(self.index)+" :"+line) op.close() print("删除前:"+str(list[0]))#已经写入过的就移除, list.remove(self.index) print(len(list)) print("删除后:"+str(list[0])) list.append(self.index)#然后在后面追加以便下一轮使用 a=[] cond=threading.Condition() for i in range(1,6): list.append(i) x=go(i) x.start() a.append(x) for j in a: j.join()

我也是小白以后多多交流
好
我也是学计算机的
给你点赞
奥利给
最好再详细点
给你点赞
奥利给
看了那么多博客,就你的能看懂
我也是学计算机的