Concurrent Shell Command Queues emmer.dev · 15 points · 5 comments · HN thread T...
HN Best 200 @best
Concurrent Shell Command Queues emmer.dev · 15 points · 5 comments · HN thread
TL;DR Blog post by emmer.dev presenting a Bash/Zsh shell-function library that builds a constrained-concurrency command queue: queue_start N creates a FIFO fed to xargs -P N, queue_submit adds commands over time, and queue_wait drains it. The author says it is semi-resilient and portable but admits hard-to-predict failure modes, and recommends Python for serious work.
- queue_start N launches an xargs -P N executor reading NUL-separated commands from a mkfifo in a private temp directory, so commands can be submitted incrementally rather than known up front.
- queue_submit writes commands into the FIFO and blocks when the queue is full; queue_wait closes the write end and waits for xargs to exit.
- Command output is captured per job and printed whole, so output from concurrent jobs does not interleave for typical sizes; exit codes are coerced to 1 to stop xargs.
- Subshells inherit the queue and different subshells can run independent queues; a shell has only one queue at a time.
- Known issues include no failure status from queue_wait, waiting on any process started after queue_start, and _queue_stop killing all descendants, including unrelated background jobs.
- The author's example use case is many HTTP requests via curl to Postman Echo, with the post noting that plain xargs -P or GNU parallel covers the case where all commands are known in advance.
Discussion The thread is small and mostly constructive. Most of the discussion is about alternatives: commenters point to make -j, which handles parallelism, skipping finished work and restarts, and offer Makefile examples mirroring the post. The mood is light and a bit wry, with one commenter defending shell concurrency primitives against the post's claim that Bash has poor concurrency controls and another complaining that shell job control is clumsy.
- Make -j is a better tool for parallel URL jobs (many): Commenters argue a Makefile gives parallelism, incremental rebuilds and skipping of already-downloaded files for free. Example rules for downloading files and for the post's curl requests were provided, run with make -O -j N.
- Make does not cover dynamically submitted commands (some): A reply concedes Make fits well when the work is known in advance, but notes the post's requirement to submit commands over time puts it in the same category as xargs -P or GNU parallel and needs something more elaborate.
- Shell has more concurrency builtins than its reputation suggests (a few): One commenter notes Bash offers &, wait and jobs, so calling it a language with poor concurrency controls is questionable.
- Bash's built-in concurrency tools are clumsy and error-prone (a few): A commenter shares a trap-based snippet that kills children on Ctrl+C and says the subshell parentheses are necessary, complaining that shell process management leads people to reinvent PID files and fragile cleanup.
- The complaint is really about programming languages in general (a few): A reply argues that difficulty with process cleanup is more an indictment of most programming languages than of the shell specifically.
- Enthusiasm for using make for interruptible, restartable tasks (a few): One commenter says they have long used make for free parallelism, interruption, restart and incremental updates.