python 多线程 7z运行异常

╄→尐↘猪︶ㄣ 提交于 2020-08-15 18:56:44
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的时候枷锁。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!