转自:http://longriver.me/?p=57python
方法1:shell
单进程处理大规模的文件速度如(上million量级)比较慢,能够采用awk取模的方法,将文件分而治之,这样能够利用充分的利用多核CPU的优点bash
for((i=0;i<5;i++));do cat query_ctx.20k | awk 'NR%5=='$i'' |\ wc -l 1> output_$i 2>err_$i & done
方法2:进程
另外也能够使用split的方法,或者hashkey 的办法把大文件分而治之,
该办法的缺陷是须要对大文件预处理,这个划分大文件的过程是单进程,也比较的耗时资源
infile=$1 opdir=querys opfile=res s=`date "+%s"` while read line do imei=`./awk_c "$line"` no=`./tools/default $imei 1000` echo $line >> $opdir/$opfile-$no done<$infile
方法3:get
该方法是方法2的延伸,在预处理以后,能够使用shell脚本起多个进程来并行执行,固然为了防止进程之间由于并行形成的混乱输出,能够使用锁的办法,也能够经过划分命名的办法。下面的例子比较巧妙使用mv 操做。这一同步操做起到互斥锁的做用,使得增长进程更加灵活,只要机器资源够用,随时增长进程,都不会形成输出上的错误。input
output=hier_res input=dbscan_res prefix1=tmp- prefix2=res- for file in `ls $input/res*` do tmp=`echo ${file#*-}` ofile1=${prefix1}${tmp} ofile2=${prefix2}${tmp} if [ ! -f $output/$ofile1 -a ! -f $output/$ofile2 ];then touch $output/aaa_$tmp mv $output/aaa_$tmp $output/$ofile1 if [ $? -eq 0 ] then echo "dealing "$file cat $file | python hcluster.py 1> $output/$ofile1 2> hier.err mv $output/$ofile1 $output/$ofile2 fi fi done