python实现多线程参考http://www.runoob.com/python/python-multithreading.html
#!/usr/bin/env python#coding:utf-8# #Description:# # #Author: LC#Date: # try: from osgeo importgdal exceptImportError: importgdal importQueue importthreading importtime importos importosr importtarfile importmath exitFlag =0
# 自定义多线程类 classmyThread(threading.Thread): def __init__(self, threadID, name, queueLock, workQueue, outputdir, demfile): threading.Thread.__init__(self) self.threadID =threadID self.name =name self.queueLock =queueLock self.workQueue =workQueue self.outputdir =outputdir self.demfile =demfile defrun(self): print "Starting " +self.name GF4_Ortho(self.name, self.queueLock, self.workQueue, self.outputdir, self.demfile) print "Exiting " +self.name defGF4_Ortho(threadName, queueLock, workQueue, outputdir, demfile): while notexitFlag: queueLock.acquire() if notworkQueue.empty(): untarfile =workQueue.get() queueLock.release() print "%s processing %s" %(threadName, untarfile) #解压 t =tarfile.open(untarfile) outdir_ = outputdir + os.path.sep + ((os.path.basename(untarfile)).split(".tar"))[0] t.extractall(path =outdir_) #计算UTM区号 filename = ((os.path.basename(untarfile)).split('.tar'))[0] lon = float(((filename.split('_'))[2])[1:]) zone_ = int(math.ceil(lon / 6)) + 30zone = int("326" +str(zone_)) printzone #正射校正 filename_ = filename.split('_') filename_[1] = 'PMS'infile = outdir_ + os.path.sep + '_'.join(filename_) + '.tiff'outfile = outdir_ + os.path.sep + '_'.join(filename_) + '_Ortho.tiff' ifos.path.exists(infile): dataset =gdal.Open(infile) dstSRS =osr.SpatialReference() dstSRS.ImportFromEPSG(zone) ret = gdal.Warp(outfile, dataset, format="Gtiff", xRes=50, yRes=50, dstSRS=dstSRS, rpc=True, transformerOptions=demfile) delt, infile, outfile, dataset, ret else: queueLock.release() time.sleep(1) defmain(inputdir, outputdir, demfile, thread_number): #查找待处理压缩文件 names =os.listdir(inputdir) filenames =[] for name_ innames: filenames.append(inputdir + os.path.sep +name_) num =len(filenames) threadList =[] for i inrange(thread_number): threadList.append("Thread-" +str(i)) queueLock =threading.Lock() workQueue =Queue.Queue(num) threads =[] threadID = 1 #创建新线程 for tName inthreadList: thread =myThread(threadID, tName, queueLock, workQueue, outputdir, demfile) thread.start() threads.append(thread) threadID += 1 #填充队列 queueLock.acquire() for name infilenames: workQueue.put(name) queueLock.release() #等待队列清空 while notworkQueue.empty(): pass #通知线程是时候退出 exitFlag = 1 #等待所有线程完成 for t inthreads: t.join() print "Exiting Main Thread" if __name__ == "__main__": indir = "H:\GF-4\2016-05-22\"outdir = "G:\Temp\GF-4\"demfile = "C:\Program Files\Exelis\ENVI53data\GMTED2010.jp2"thread_num = 6 starttime =time.ctime() main(indir, outdir, demfile, thread_num) endtime =time.ctime() print "Start time:", starttime print "End time:", endtime