From 5479dfabbe92c42ab3bc22195df845a9a5a004a5 Mon Sep 17 00:00:00 2001 From: Sven Finke Date: Thu, 21 Jul 2022 21:20:35 +0200 Subject: [PATCH] add parallelism --- README.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f2a893..46fa748 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ - [Useful readline tricks](#useful-readline-tricks) - [Repeat command](#repeat-command) - [Substrings](#substrings) +- [Parallelism](#parallelism) - [More resources](#more-resources) ## Switch to previous directory @@ -244,7 +245,37 @@ Execute a command every two seconds and monitor its` output. This is especially useful for waiting until a deployment or infrastructure provisioning is completed, i.e. on aws. `watch -n2 echo hello` -` + +## Parallelism + +### Run everything at once + +``` +task(){ + sleep 0.5; echo "$1"; +} + +for thing in a b c d e f g; do + task "$thing" & +done +``` + +### Run N tasks in parallel + +``` +task(){ + sleep 0.5; echo "$1"; +} + +N=4 +( +for thing in a b c d e f g; do + ((i=i%N)); ((i++==0)) && wait + task "$thing" & +done +) +``` + ## Substrings ```