Я пытаюсь улучшить производительность скрипта, который анализирует данные из больших текстовых файлов (1-100gb). Я думал, что буду многопроцессорным, чтобы посмотреть, не ускорит ли это. Насколько я могу судить, процессы начинаются нормально, но это примерно в 3 раза медленнее, чем сохранение без многопроцессорности.Выход многопроцессорного файла значительно медленнее
Версия многопроцессорная:
from multiprocessing import Lock, Process
from datetime import datetime
def worker(mylist, count):
outFile = str(count) + '.txt'
out = open(outFile,'w')
for i in mylist:
out.write(i)
def main():
## lock = Lock()
startTime = datetime.now()
jobs = []
tempList = []
count = 0
inFile = open('batch1.kscsv','r')
for line in inFile:
if('Traversal' in line and len(tempList) == 0):
traversalString = line
if('Traversal' not in line and 'Spot' not in line and 'XValue' not in line):
line = line.replace(',',' ')
tempList.append(line)
if('Traversal' in line and len(tempList) > 0):
spotFromFile = (traversalString.split(',')[1]).strip()
count += 1
p = Process(target=worker, args=(tempList, count,))
p.start()
tempList = []
traversalString = line
print ('Run took: ' + str((datetime.now()-startTime)))
if __name__ == '__main__':
main()
Очередной сценарий:
from datetime import datetime
def main():
startTime = datetime.now()
jobs = []
tempList = []
count = 0
inFile = open('batch1.kscsv','r')
for line in inFile:
if('Traversal' in line and len(tempList) == 0):
traversalString = line
if('Traversal' not in line and 'Spot' not in line and 'XValue' not in line):
line = line.replace(',',' ')
tempList.append(line)
if('Traversal' in line and len(tempList) > 0):
spotFromFile = (traversalString.split(',')[1]).strip()
count += 1
outFile = str(count) + '.txt'
out = open(outFile,'w')
for i in tempList:
out.write(i)
tempList = []
traversalString = line
print ('Run took: ' + str((datetime.now()-startTime)))
if __name__ == '__main__':
main()
Является ли это проблемой, что это просто не очень хорошо подходит для многопроцессорных? Или есть способ улучшить многопроцессорность?
Coud вы загружаете файл 'batch1.kscsv' где-нибудь, чтобы я мог проверить его на своей машине. – laike9m
@ laike9m Файл в основном повторение ху координат, как: Traversal Index, 256 XValue: Дальтон, YValue 2998.18398466268,0.01116455 2998.43673408455,0.01067383 2998.68949776728,0.01160645 2998.94227571129,0.01089355 2999.19506791702,0.01139648 2999.44787438491,0.01166748 2999.70069511539, 0.01152344 2999.95353010889,0.0110791 – user2267354