아래의 스크립트를 실행하여 파일 g의 도메인에 대해 파일 f에서 IP 주소를 추출합니다. 이 파일은 경로의 11 개 파일이고 각 파일에는 약 8 억 개의 행 (각 파일 f)이 있음을 언급 할 필요가 있습니다. 이 스크립트에서 나는 메모리에있는 사전 d에 파일 g를로드 할 때 d의 항목과 비교하여 파일 f의 행을 비교합니다. d의 bl_date가 f의 날짜 사이에 있는지 확인합니다. 그것을 다른 사전 dns_dic에 씁니다. 여기 Python : 왜이 스크립트는 어느 시점 이후에 매우 느려지 는가?
path = '/data/data/2014*.M.mtbl.A.1'
def process_file(file):
start = time()
dns_dic=defaultdict(set)
d = defaultdict(set)
filename =file.split('/')[-1]
print(file)
g = open ('/data/data/ap2014-2dom.txt','r')
for line in g:
line = line.strip('\n')
domain, bl_date= line.split('|')
bl_date = int(bl_date)
if domain in d:
d[domain].add(bl_date)
else:
d[domain] = set([bl_date])
print("loaded APWG in %.fs" % (time()-start))
stat_d, stat_dt = 0, 0
f = open(file,'r')
with open ('/data/data/overlap_last_%s.txt' % filename,'a') as w:
for n, line in enumerate(f):
line=line.strip('')
try:
jdata = json.loads(line)
dom = jdata.get('bailiwick')[:-1]
except:
pass
if dom in d:
stat_d += 1
for bl_date in d.get(dom):
if jdata.get('time_first') <= bl_date <= jdata.get('time_last'):
stat_dt += 1
dns_dic[dom].update(jdata.get('rdata', []))
for domain,ips in dns_dic.items():
for ip in ips:
w.write('%s|%s\n' % (domain,ip))
w.flush()
if __name__ == "__main__":
files_list = glob(path)
cores = 11
print("Using %d cores" % cores)
pp = Pool(processes=cores)
pp.imap_unordered(process_file, files_list)
pp.close()
pp.join()
이 파일 (F)입니다 : 여기
{"bailiwick":"ou.ac.","time_last": 1493687431,"time_first": 1393687431,"rdata": ["50.21.180.100"]}
{"bailiwick": "ow.ac.","time_last": 1395267335,"time_first": 1395267335,"rdata": ["50.21.180.100"]}
{"bailiwick":"ox.ac.","time_last": 1399742959,"time_first": 1393639617,"rdata": ["65.254.35.122", "216.180.224.42"]}
파일 g된다
ou.ac|1407101455
ox.ac|1399553282
ox.ac|1300084462
ox.ac|1400243222
예상 결과 :
ou.ac|["50.21.180.100"]
ox.ac|["65.254.35.122", "216.180.224.42"]
수 여기처럼 내 스크립트 모습입니다 누군가 내가 왜 시간의 어떤 시점에서 스크립트는 메모리 사용량이 약 400 MG이지만 항상 느려집니다.
한 줄에 전체 파일을 한꺼번에 여는 이유가 있습니까? –
파일 g를 원한다면 예 (d)를 한 번로드 한 다음 파일 f를이 사전에 줄 단위로 비교합니다. d. – UserYmY
큰 파일이 어느 것입니까? 경로의 파일이 –