Многопоточность скрипта Bash

2487
Liviu ZeJah

У меня есть текстовый файл 8 ГБ, и я должен запустить скрипт Python для каждой строки в файле и сохранить часть вывода.

Есть ли способ, которым я могу разделить это на несколько процессов, чтобы заставить его работать быстрее, в bash?

Вот мой текущий скрипт bash:

#!/bin/bash filename='before.txt' while read p; do  python py-hex.py $p | sed -n -e '/^qter/p' | sed 's/qter: //g' >> converted.txt done < $filename 
0

1 ответ на вопрос

2
davidgo

I think you need to provide more detail on the limits - for example, does the output in converted.txt need to be in the same order as 'before.txt', how long does each parse of the python script take ? If the output order is not contingent on the input, you may be able to do this by backgrounding the processes and launching a number of them in each loop - the number depending, I guess, on how many threads your CPU will handle.

Something like the following might (or might not) suit your purpose:

#! /bin/bash threads=4; threads=$(( $threads - 1)) while read filein do python py-hex.py $filein | sed -n -e '/^qter/p' | sed 's/qter: //g' >> converted.txt & for thread in `seq $threads` do read filein python py-hex.py $filein | sed -n -e '/^qter/p' | sed 's/qter: //g' >> converted.txt & done done < $filename 

Notes: This assumes your python file can handle empty inputs (ie if the number of commands not exactly divisible by the number of threads there will be some empty lines - you could always do a check for this before executing the inner loop.

This script assumes you don't care about the output order.

да, что-то в этом роде. работает отлично . все еще тестирую это, но, кажется, работает немного быстрее. Спасибо за вашу помощь! Liviu ZeJah 8 лет назад 0

Похожие вопросы