25个线程要做完1000个任务
import threading,time,random,os,threadpool
from shutil import copyfile
def dealing(cata):
print threading.current_thread().name
print 'entering ',cata
os.popen('sh DORFTN '+cata).read()
# os.chdir('..')
if __name__ == "__main__":
cata=[c for c in os.listdir('.') if os.path.isdir(c)]
pool=threadpool.ThreadPool(25)
requests=threadpool.makeRequests(dealing,cata)
[pool.putRequest(req) for req in requests]
pool.wait()
如果函数内有多个参数,可如下处理:
import threadpool, os, glob
out=[]
def processing(sample,trace):
tmp=sample.split('/')
tracefile=trace+'/'+tmp[-1]
ret=os.popen('./doublet_search %s %s' % (sample,tracefile)).read()
print(ret)
if __name__=='__main__':
traces=glob.glob('Trace/2*')
samples=glob.glob('Template/2*')
arglist=[]
for sample in samples:
Zs=glob.glob(sample+'/*.?HZ')
for Z in Zs:
sam=Z[0:-1]
for trace in traces:
arglist.append(([sam,trace],None))
# print(arglist)
pool_t=threadpool.ThreadPool(8)
requests=threadpool.makeRequests(processing,arglist)
[pool_t.putRequest(req) for req in requests]
pool_t.wait()
****注:一般在函数中不要用到os.chdir,因为chdir是全局的,如果要用,则要用绝对路径。
Post Views: 477