bash 中的多处理

问题描述 投票:0回答:1

我在一个 python 项目上编写了一个脚本(包装器)。 这是链接: https://github.com/daniellerch/aletheia https://daniellerch.me/stego/aletheia/intro-en/#aletheia-commands 该脚本的本质是用户使用 4 中的方法 1 并指示图像的路径(有数万个图像)。每张照片的结果都会输入到 csv 文件中。 我有一台80核的计算服务器。所以问题是我怎样才能使照片不被一张一张地处理,而是例如一次80张+然后一次另外80张照片,直到所有照片完成???

这是我的 bash 脚本:

#!/bin/bash

echo ___________________________________________
echo "[I] Select the structural type of steganalysis:"
echo ___________________________________________
echo [1] ws
echo [2] rs
echo [3] triples
echo [4] spa
stuct=
read -p "Enter the steganalysis structure type number: " struct_tmp
if ((struct_tmp == 1)); then
  struct="ws"
elif ((struct_tmp == 2)); then
  struct="rs"
elif ((struct_tmp ==3)); then
  struct="triples"
elif ((struct_tmp == 4)); then
  struct="spa"
else
  echo "Run the script again and enter the structure method number from 1 to 4!"
  exit
fi
echo "Structural steganalysis method $struct selected!"
echo _________________________________________________
echo "[II] Enter the path to the directory with images:"
echo _________________________________________________
read in_path
echo "Path to the directory with images ($in_path)"
#mkdir csv_result
awk 'BEGIN {OFS=","; print "FILE-NAME, '"$struct"'-channel-R, '"$struct"'-channel-G, '"$struct"'-channel-B"}' >out_$struct.csv 

for img in "$in_path"/*
do
  echo treatment $(basename $img) ... 
  ./aletheia.py $struct $img | awk -v img_awk="$(basename $img)" '
    /Hidden data found in channel R/ { R = $7 }
    /Hidden data found in channel G/ { G = $7 }
    /Hidden data found in channel B/ { B = $7 }
    END {OFS=","; print img_awk, R, G, B}' >>out_$struct.csv 
  echo 
done
echo _______________________________________________________________
echo "[III] CSV file created <out_$struct.csv>"
echo "with the results of the structural method-$struct steganalysis report!"
echo _______________________________________________________________

我认为这可以使用函数和“&”来完成,但是如何移动目录并将数据写入文件?

bash multithreading shell unix multiprocessing
1个回答
0
投票

xargs -P n
将成为你的朋友。它就像常规的
xargs
,但使用
n
进程进行并行化。

您可以在单独的脚本中编写每个图像的处理,比方说,

process-single-image.sh
,然后:

find "$in_path" -maxdepth 1 -type f | xargs -P 32 process-single-image.sh >> out.csv

我一直使用这种结构,通常与常规 shell 命令一起使用。例如,要快速

gunzip
一大堆文件:

find . -type f -name '*.gz' | xargs -P 64 gunzip
© www.soinside.com 2019 - 2024. All rights reserved.