#encoding=utf-8 import threading import random import time import os from Queue import Queue from time import sleep,ctime sourDir = "F:\\tmp\\fr" descDir = "F:\\tmp\\se" lentDir = 0 consumerList = [] consumerName = [] MAXTHREAD = 25 class Producer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): alllist = os.listdir(sourDir) pro = 0 for item in alllist: #print self.getName(),'adding',item,'to queue' #print pro #pro = pro + 1 self.sharedata.put(item) print self.getName(),'Finished' # Consumer thread class Consumer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): global lentDir #print self.getName(),lentDir con = 0 for i in range(lentDir/25): #print self.getName(),con #con = con + 1 #print self.getName(),'got a value:',self.sharedata.get() cmd = "move " + "\"" + sourDir + "\\" + self.sharedata.get() + "\"" + " " + "\"" + descDir + "\"" + " >>nul " #print cmd os.system(cmd) #print self.getName(),'Finished' def main(): alllist = os.listdir(sourDir) global lentDir consumer = [] lentDir = len(alllist) for i in range(MAXTHREAD): consumerName.append('Consumer'+ str(i)) consumerList.append('consumer'+str(i)) queue = Queue() producer = Producer('Producer', queue) for i in range(MAXTHREAD): consumerList[i] = Consumer(consumerName[i], queue) #consumer1 = Consumer('Consumer1', queue) #consumer2 = Consumer('consumer2', queue) print 'Starting copying ...',ctime() producer.start() #start the sub thread for i in range(MAXTHREAD): consumerList[i].start() #consumer1.start() #consumer2.start() producer.join() for i in range(MAXTHREAD): consumerList[i].join() #consumer1.join() #consumer2.join() print 'All threads finished.',ctime() if __name__ == '__main__': main()
~!@#qwer
最新推荐文章于 2024-12-23 13:48:18 发布