2011-06-25 47 views
3

其對 http://en.wikipedia.org/wiki/Parallel_(software) 和非常豐富的聯機幫助http://www.gnu.org/software/parallel/man.html慶典GNU平行幫助

(for x in `cat list` ; do 
     do_something $x 
    done) | process_output 

由該

cat list | parallel do_something | process_output 

我想實現,在這個

while [ "$n" -gt 0 ] 
     do   
     percentage=${"scale=2;(100-(($n/$end) * 100))"|bc -l}} 
    #get url from line specified by n from file done1    
nextUrls=`sed -n "${n}p" < done1` 
    echo -ne "${percentage}% $n/$end urls saved going to line 1. current: $nextUrls\r" 
# function that gets links from the url 
    getlinks $nextUrls 
#save n 
    echo $n > currentLine 
    let "n--" 
    let "end=`cat done1 |wc -l`" 
    done 
取代

同時閱讀文檔GNU平行

我發現功能不支持這樣getlinks不會在使用平行

最好我迄今發現是

seq 30 | parallel -n 4 --colsep ' ' echo {1} {2} {3} {4} 

使得輸出

1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 
17 18 19 20 
21 22 23 24 
25 26 27 28 
29 30 

while循環上面提到的應該是這樣,如果我是對的

end=`cat done1 |wc -l` 
seq $end -1 1 | parallel -j+4 -k 
#(all exept getlinks function goes here, but idk how?)| 
# everytime it finishes do 
getlinks $nextUrls 

thx提前幫助

+0

你究竟想做什麼,以及你的問題是什麼? –

回答

5

看來你想要的是一個進度表。嘗試:

cat done1 | parallel --eta wget 

如果這不是你想要的,看semsem是一個別名爲parallel --semaphore,並且通常使用GNU並行安裝):

for i in `ls *.log` ; do 
    echo $i 
    sem -j+0 gzip $i ";" echo done 
done 
sem --wait 

你的情況,這將是什麼像:

while [ "$n" -gt 0 ] 
    do   
    percentage=${"scale=2;(100-(($n/$end) * 100))"|bc -l}} 
    #get url from line specified by n from file done1 
    nextUrls=`sed -n "${n}p" < done1` 
    echo -ne "${percentage}% $n/$end urls saved going to line 1. current: $nextUrls\r" 
    # function that gets links from the url 
    THE_URL=`getlinks $nextUrls` 
    sem -j10 wget $THE_URL 
    #save n 
    echo $n > currentLine 
    let "n--" 
    let "end=`cat done1 |wc -l`" 
done 
sem --wait 
echo All done 
+0

thx,但看看我正在尋找我的下一個答案, – nkvnkv

1

爲什麼getlinks需要是一個函數?把這個函數轉換成一個shell腳本(應該是基本相同的,除非你需要導出環境變量,並且你當然不會在沒有大量工作的情況下影響外部環境)。

當然,在試圖並行執行時,不能將$ n保存到當前行。所有文件將同時覆蓋對方。

+0

我在'getlinks sort -f done1「>」done2 \t uniq done2「>」done1'得到了這個結果sort如果排序並刪除重複的urls – nkvnkv

+0

只有問題是做一個並行做這個:percentage = $ {「scale = 2;(100 - (($ n/$ end)* 100))「| bc -l}} #get url from file done1 nextUrls ='sed -n」$ {n} p「 nkvnkv

+0

我不在乎如果我閱讀ñ它的20或30,因爲我有10個平行在同一時間,但問題是,如果我保存idk 10和最後一行應該是5000 – nkvnkv

0

我正在考慮做更像這樣的東西,如果不是平行或山姆別的東西,因爲平行不支持f uncitons又名http://www.gnu.org/software/parallel/man.html#aliases_and_functions_do_not_work

getlinks(){ 
if [ -n "$1" ] 
then 
    lynx -image_links -dump "$1" > src 
    grep -i ".jpg" <src> links1 
    grep -i "http" <links1>links 
    sed -e 's/.*\(http\)/http/g' <links>> done1 
    sort -f done1 > done2 
    uniq done2 > done1 
    rm -rf links1 links src done2 
fi 
} 
func(){ 
percentage=${"scale=2;(100-(($1/$end) * 100))"|bc -l}} 
     #get url from line specified by n from file done1 
     nextUrls=`sed -n "${$1}p" < done1` 
     echo -ne "${percentage}% $n/$end urls saved going to line 1. current: $nextUrls\r" 
     # function that gets links from the url 
     getlinks $nextUrls 
     #save n 
     echo $1 > currentLine 
     let "$1--" 
     let "end=`cat done1 |wc -l`" 
} 
while [ "$n" -gt 0 ] 
    do   
    sem -j10 func $n 
done 
sem --wait 
echo All done 

我的劇本已經變得非常複雜,我不想做出特色的東西不可用,我不知道這是可以做到 這樣我可以得到完整的互聯網業務的鏈路被使用,應該採取更短的時間這樣

+0

如果你做'func'和'ge tLinks'作爲一個腳本而不是一個函數,那麼上面的代碼應該稍作修改:你在getLinks中顯然有競爭條件,所以你需要確保你保存的文件被稱爲獨特的東西。 –

0

tryed SEM

#!/bin/bash 
func(){ 
echo 1 
echo 2 
} 


for i in `seq 10` 
do 
sem -j10 func 
done 
sem --wait 
echo All done 

錯誤

Can't exec "func": No such file or directory at /usr/share/perl/5.10/IPC/Open3.p 
m line 168. 
open3: exec of func failed at /usr/local/bin/sem line 3168 
+0

上帝以任何方式在bash中執行管道功能 – nkvnkv

+0

'sem'是'parallel --semaphore'的別名,因此對函數/別名具有相同的限制。 –

0

腳本的最終目標是什麼並不十分清楚。如果您正在嘗試編寫並行Web爬網程序,則可以將以下內容用作模板。

#!/bin/bash 

# E.g. http://gatt.org.yeslab.org/ 
URL=$1 
# Stay inside the start dir 
BASEURL=$(echo $URL | perl -pe 's:#.*::; s:(//.*/)[^/]*:$1:') 
URLLIST=$(mktemp urllist.XXXX) 
URLLIST2=$(mktemp urllist.XXXX) 
SEEN=$(mktemp seen.XXXX) 

# Spider to get the URLs 
echo $URL >$URLLIST 
cp $URLLIST $SEEN 

while [ -s $URLLIST ] ; do 
    cat $URLLIST | 
    parallel lynx -listonly -image_links -dump {} \; wget -qm -l1 -Q1 {} \; echo Spidered: {} \>\&2 | 
    perl -ne 's/#.*//; s/\s+\d+.\s(\S+)$/$1/ and do { $seen{$1}++ or print }' | 
    grep -F $BASEURL | 
    grep -v -x -F -f $SEEN | tee -a $SEEN > $URLLIST2 
    mv $URLLIST2 $URLLIST 
done 

rm -f $URLLIST $URLLIST2 $SEEN