Skip to content

Instantly share code, notes, and snippets.

@sixg0000d
Created May 20, 2021 14:11
Show Gist options
  • Save sixg0000d/1fcea2efc49c0f3fd323d61dbfae692d to your computer and use it in GitHub Desktop.
Save sixg0000d/1fcea2efc49c0f3fd323d61dbfae692d to your computer and use it in GitHub Desktop.
fish 多线程执行命令的方法
#!/usr/bin/fish
# thread nums
set num 10
for i in (seq $num)
echo $num
end | xargs -L 1 -P $num sh -c '<command you want to run>'

fish shell 的 & 实现和 bash 貌似有差异,不能通过将进程藏进后台的方式实现多线程,且将将进程藏进后台的方式来实现多线程本身就不是特别靠谱,只是很简单。我们可以使用 xargs 这一常用命令实现多线程,例如:

for i in (seq 10)
    echo $i
end | xargs -L 1 -P 10 sh -c 'echo $0 && sleep 3'

# 可以看出输出是乱序的
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment