高效的并行下载和解压缩与匹配模式的文件列表在服务器上



每天每6个小时,我都要从web服务器下载bz2文件,对它们进行解压缩并合并为一个文件。这需要尽可能高效和快速,因为在继续进行合并之前,我必须等待下载和解压缩阶段完成。

我已经编写了一些bash函数,这些函数以一些字符串作为输入,构建要下载的文件的URL作为匹配模式。通过这种方式,我可以将匹配模式直接传递给wget,而不必在本地构建服务器的内容列表,然后将其作为具有-iwget的列表传递。我的功能看起来像这个

parallelized_extraction(){
i=0
until [ `ls -1 ${1}.bz2 2>/dev/null | wc -l ` -gt 0 -o $i -ge 30 ]; do
((i++))
sleep 1
done
while [ `ls -1 ${1}.bz2 2>/dev/null | wc -l ` -gt 0 ]; do
ls ${1}.bz2| parallel -j+0 bzip2 -d '{}' 
sleep 1
done
}
download_merge_2d_variable()
{
filename="file_${year}${month}${day}${run}_*_${1}.grib2"
wget -b -r -nH -np -nv -nd -A "${filename}.bz2" "url/${run}/${1,,}/"
parallelized_extraction ${filename}
# do the merging 
rm ${filename}
} 

我称之为download_merge_2d_variable name_of_variable我能够通过编写函数parallelized_extraction来加快代码的速度,该函数负责在wget在后台运行时对下载的文件进行解压缩。为此,我首先等待第一个.bz2文件出现,然后运行并行提取,直到服务器上出现最后一个.bz2(这就是两个untilwhile循环正在做的事情(。

我对这种方法很满意,但我认为它可以改进。以下是我的问题:

  • 如果我的文件列表是匹配模式,我如何启动wget的多个实例来执行并行下载?我必须用";块";内部的数据,还是我必须从服务器下载一个内容列表,拆分该列表,然后将其作为wget的输入
  • 如果文件的下载真的很慢,parallelized_extraction可能会失败,因为它不会找到任何新的bz2文件来提取并在下一次迭代时退出循环,尽管wget仍在后台运行。虽然这从未发生在我身上,但这是有可能的。为了解决这个问题,我试图在第二个条件中添加一个条件,让wgetPID在后台运行,以检查它是否仍然存在,但不知何故它不起作用
parallelized_extraction(){
# ...................
# same as before ....
# ...................
while [ `ls -1 ${1}.bz2 2>/dev/null | wc -l ` -gt 0 -a kill -0 ${2} >/dev/null 2>&1 ]; do
ls ${1}.bz2| parallel -j+0 bzip2 -d '{}' 
sleep 1
done
}
download_merge_2d_variable()
{
filename="ifile_${year}${month}${day}${run}_*_${1}.grib2"
wget -r -nH -np -nv -nd -A "${filename}.bz2" "url/${run}/${1,,}/" &
# get ID of process running in background
PROC_ID=$!
parallelized_extraction ${filename} ${PROC_ID}
# do the merging
rm ${filename}
}

有没有线索表明为什么这不起作用?关于如何改进我的代码,有什么建议吗?感谢

更新我在这里发布了我的工作解决方案,以防有人感兴趣。

# Extract a plain list of URLs by using --spider option and filtering
# only URLs from the output 
listurls() {
filename="$1"
url="$2"
wget --spider -r -nH -np -nv -nd --reject "index.html" --cut-dirs=3 
-A $filename.bz2 $url 2>&1
| grep -Eo '(http|https)://(.*).bz2'
}
# Extract each file by redirecting the stdout of wget to bzip2
# note that I get the filename from the URL directly with
# basename and by removing the bz2 extension at the end 
get_and_extract_one() {
url="$1"
file=`basename $url | sed 's/.bz2//g'`
wget -q -O - "$url" | bzip2 -dc > "$file"
}
export -f get_and_extract_one
# Here the main calling function 
download_merge_2d_variable()
{
filename="filename.grib2"
url="url/where/the/file/is/"
listurls $filename $url | parallel get_and_extract_one {}
# merging and processing
}
export -f download_merge_2d_variable_icon_globe

您能列出要下载的URL吗?

listurls() {
# do something that lists the urls without downloading them
# Possibly something like:
# lynx -listonly -image_links -dump "$starturl"
# or
# wget --spider -r -nH -np -nv -nd -A "${filename}.bz2" "url/${run}/${1,,}/"
# or
# seq 100 | parallel echo ${url}${year}${month}${day}${run}_{}_${id}.grib2
}
get_and_extract_one() {
url="$1"
file="$2"
wget -O - "$url" | bzip2 -dc > "$file"
}
export -f get_and_extract_one
# {=s:/:_:g; =} will generate a file name from the URL with / replaced by _
# You probably want something nicer.
# Possibly just {/.}
listurls | parallel get_and_extract_one {} '{=s:/:_:g; =}'

通过这种方式,您将在下载和并行执行所有操作的同时进行解压缩。

最新更新