多进程、多线程、单进程处理文件-学习记录

  • Post author:
  • Post category:其他


单进程

import csv,shutil

import os
import sys

if len(sys.argv) != 2:
    print('check paramter')
    sys.exit()

dirname = sys.argv[-1]
srcdir = './%s'%dirname
desdir = './%s_txt'%dirname

if not os.path.exists('%s'%desdir):
     os.mkdir('%s'%desdir)
files = os.listdir(srcdir)
for filename in files:
    print(filename)
    a=open('%s/%s'%(srcdir,filename),'r')
    reader = csv.reader(a)
    with open('%s/%s.txt'%(desdir,filename.split('.csv')[0]),'w') as f:
        for i in reader:
            for x in i:
                f.write(x)
                f.write('\x01')
            f.write('\n')
        a.close()

多进程

import csv,shutil

from multiprocessing.dummy import Pool as ThreadPool
import os
import sys
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor


if len(sys.argv) != 2:
    print('check paramter')
    sys.exit()

dirname = sys.argv[-1]
srcdir = './%s'%dirname
desdir = './%s_txt'%dirname

if not os.path.exists('%s'%desdir):
     os.mkdir('%s'%desdir)
files = os.listdir(srcdir)

def process(files):
    print(files)
    a=open('%s/%s'%(srcdir,files),'r')
    reader = csv.reader(a)
    with open('%s/%s.txt'%(desdir,files.split('.csv')[0]),'w') as f:
        for i in reader:
            for x in i:
                f.write(x)
                f.write('\x01')
            f.write('\n')
        a.close()


if __name__=="__main__": 
    pool = ProcessPoolExecutor(max_workers=8)
    for i in files:
        future = pool.submit(process, i)
    pool.shutdown()

多线程

import csv,shutil

from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Process

import os
import sys

if len(sys.argv) != 2:
    print('check paramter')
    sys.exit()

dirname = sys.argv[-1]
srcdir = './%s'%dirname
desdir = './%s_txt'%dirname

if not os.path.exists('%s'%desdir):
     os.mkdir('%s'%desdir)
files = os.listdir(srcdir)



def process(files):
    print(files)
    a=open('%s/%s'%(srcdir,files),'r')
    reader = csv.reader(a)
    with open('%s/%s.txt'%(desdir,files.split('.csv')[0]),'w') as f:
        for i in reader:
            for x in i:
                f.write(x)
                f.write('\x01')
            f.write('\n')
        a.close()

if __name__=="__main__": 
    pool = ThreadPool() ###线程池
    pool.map(process, files)
    pool.close()
    pool.join()



版权声明:本文为chl183原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。