이전에 작성했던 코드를 다시 한번 수정했다. 별다른 것은 없고 퍼포먼스 체크를 위해 실행 시간을 출력하는 부분을 상점ID별로 정보를 출력하도록 수정했다.
# _*_ coding: utf-8 _*_ from multiprocessing import Process import time import datetime import locale seller = [] exclude = [] datas = [] def get_exce_time(start, end): return datetime.timedelta(seconds=(end - start)) def number_format(num, places=0): """Format a number according to locality and given places""" locale.setlocale(locale.LC_ALL, "") return locale.format("%.*f", (places, num), True) def file_read(): start = time.time() results = {} f = open('seller_id.txt', 'r', encoding="utf-8") lines = f.readlines() f.close() for line in lines: str = line.split("\t") id = str[4].strip() if id not in seller: seller.append(id) results['seller'] = seller f = open('exclude_item_id.txt', 'r', encoding="utf-8") lines = f.readlines() f.close() for line in lines: str = line.strip() if str not in exclude: exclude.append(str) results['exclude'] = exclude f = open('AllDataBasedOnDB.dat', 'r', encoding="utf-8") datas = f.readlines() f.close() results['datas'] = datas end = time.time() print("File Reading Time : {0}".format(get_exce_time(start, end))) return results def find_item_code(data, process, idx, limit): seller = data['seller'] exclude = data['exclude'] datas = data['datas'] for k, s_id in enumerate(seller): start = time.time() item = [] if k % process != idx: continue for line in datas: if limit > 0 and len(item) == limit: break if line.find(s_id) == -1: continue str = line.split("\t") code = str[0].strip() if code not in exclude and code not in item: item.append(code) # txt 파일생성 f = open('./out/' + s_id + '.txt', 'w') f.writelines(map(lambda x: x + "\n", item)) f.close() end = time.time() print("{0} Process #{1} Running Time : {2} Count: {3}".format(s_id, idx, get_exce_time(start, end), number_format(len(item)))) if __name__ == '__main__': data = file_read() # 실행 프로세스 수 process = 4 # 추출한 레코드 수 0으로 설정하면 모든 레코드 추출 limit = 0 procs = [Process(target=find_item_code, args=((data, process, i, limit,))) for i in range(process)] for p in procs: p.start()
이젠 이 코드를 가지고 Class로 변경하는 걸 해보고 싶은데.. 언제가 될지는 아직 모르겠다.