import os
import logging
from multiprocessing.dummy import Pool
#from multiprocessing import Pool
n = 20
datFolder = 'dat'
zipFolder = 'zip'
def initFolder():
for folder in [datFolder,zipFolder]:
if not os.path.isdir(folder):
os.mkdir(folder)
else:
for f in os.listdir(folder):
os.remove(os.path.join(folder,f))
def work(i):
filename = os.path.join(datFolder,'{idx}.dat'.format(idx=i))
with open(filename,'wb') as f:
a = [chr((n+i) & 0xFF) for n in xrange(0,1024*104*5)]
f.write(''.join(a))
dst = os.path.join(zipFolder, '{idx}.zip'.format(idx=i))
cmd = '7z a {dst} {src}'.format(dst=dst, src=filename)
logging.error(cmd)
os.system(cmd)
def main():
initFolder()
p = Pool(4)
tasks = [p.apply_async(work, (i,)) for i in xrange(0,n)]
results = [task.get() for task in tasks]
print('finish')
if __name__ == '__main__':
main()
测试代码如上.
用多进程正常,或者多线程在运行7Z的时候枷锁。
来源:oschina
链接:https://my.oschina.net/u/4332788/blog/4424229