News for hackers. Stuff that matters.
Re:An Opinionated Guide to Xargs by chubot on Saturday August 21, 2021 @01:22PM (#28258749)
I mention it here: https://www.oilshell.org/blog/2021/08/xargs.html#xargs-p-aut...

What does it do that xargs and shell can't? (honest question)

Re:An Opinionated Guide to Xargs by comex on Saturday August 21, 2021 @01:49PM (#28258970)
One thing parallel can do better than xargs is collect output.

If you use `xargs -P`, all processes share the same stdout and output may be mixed arbitrarily between them. (If the program being executed uses line buffering, lines usually won't be mixed together from multiple invocations, but they can be if they're long enough).

In contrast, `parallel` by default doesn't mix together output from different commands at all, instead buffering the entire output until the command exits and then printing it.

With `--line-buffer` the unit of atomicity can be weakened from an entire command output to individual lines of output, reducing latency.

Alternately, with `--keep-order`, `parallel` can ensure the outputs are printed in the same order as the corresponding inputs, which makes the output deterministic if the program is deterministic. Without that you'll get results in an arbitrary order.

These aren't technically things that xargs and shell can't do; you could reimplement the same behavior by hand with the shell. But by the same token, there isn't anything xargs can do that the shell can't do alone; you could always use the shell to manually split up the input and invoke subprocesses. It's just a question of how much you want to reimplement by hand.

Re:An Opinionated Guide to Xargs by chubot on Saturday August 21, 2021 @02:52PM (#28259473)
OK thanks, looks like there are several features of GNU parallel that users like.

For the output interleaving issue, what I do is use the $0 Dispatch Pattern and write a shell function that redirects to a file:

    do_one() {
      task_with_stdout > $dir/$task_id.txt
    }
So if there are 10,000 tasks then I get 10,000 files, and I can check the progress with "ls", and I can also see what tasks failed and possibly restart them.

You even have some notion of progress by checking the file size with ls -l.

I tend to use a pattern where each task also outputs a metadata file: the exit status, along with the data from "time" (rusage, etc.)

But I admit that this is annoying to rewrite in every script that uses xargs! It does make sense to have this functionality in a tool.

But I think that tool should be a LANGUAGE like Oil, not a weirdo interface like GNU parallel :)

But thanks for the explanation (and thanks to everyone in this subthread) -- I learned a bunch and this is why I write blog posts :)

Re:An Opinionated Guide to Xargs by Godel_unicode on Saturday August 21, 2021 @03:12PM (#28259658)
Thank you for writing this, it really crystalized for me why I feel the way I do about oil. I hate it. When I want a language, I want a real language like python not a weirdo jumped up shell (see what I did there?). What I want in a shell is a super small, fast, universally understood thing for basic tasks and easy expandability through tools like parallel and python.

For what it's worth, I consider oil to be closer to a unixy PowerShell rather than a more powerful bash. Note that this is not a slight, PowerShell is sweet for what it is. It (oil) really takes a hard left from the POSIX philosophy of focusing on one thing and doing it well. I'm also bitter that, if it's going to veer so far away from POSIX, that it didn't go the whole hundred and become a function language with comprehensions and such.

For what it's worth, everything you mentioned above about your approach can be done with parallel.

Re:An Opinionated Guide to Xargs by cben on Monday August 30, 2021 @02:38AM (#28353268)
The point of oil that there are really basic things like safe quoting that shells should do well, yet none of the posix shell do!

Functional: there are interesting shells like Elvish. But it really goes PowerShell by adding internal rich data pipelines that dont have a unixy stream-of-bytes representation. Oil does NOT go that way; it works on stuff like QSN to make pure unix interconnects more robust.

Re:An Opinionated Guide to Xargs by MainJane on Saturday August 21, 2021 @09:54PM (#28262220)
Your `do_one`:

  * does not buffer stderr
  * does not check if the disk is full for a period of time during a task (thus risking incomplete output)
  * does not clean up, if killed
  * does not work correctly if task_with_stdout is a composed command
Given that GNU Parallel is a drop-in replacement for xargs, I am curious why you find it a 'weirdo interface'.
Re:An Opinionated Guide to Xargs by michaelcampbell on Sunday August 22, 2021 @01:02PM (#28267229)
Any sufficiently NIH'd can be considered weird. -- Not Isaac Asimov
Re:An Opinionated Guide to Xargs by scottlamb on Saturday August 21, 2021 @07:12PM (#28261411)
A lot of this comes down to familiarity. I tend to use "make -j 100" for what you're describing. If I write the Makefile carefully [1], it will handle resuming a half-finished job. I just looked and GNU parallel has a --resume argument which probably does something similar, and maybe with less hassle. But I don't do this often enough—and/or GNU parallel isn't "better enough"—that I'm likely to ever invest the time to learn GNU parallel.

btw, oil looks very cool. I hate how many footguns are in common shells.

[1] eg writing to a tempfile and atomically renaming into place: "task_with_stdout > $dir/task_id.txt.tmp && mv $dir/task_id.txt{.tmp,}"

Re:An Opinionated Guide to Xargs by xmcqdpt2 on Saturday August 21, 2021 @02:42PM (#28259389)
Restart capability and remote executions make gnu parallel the tool if choice for HPC. For example, you might very well use gnu parallel to run 1000s of cpu-hours of numerical simulation using patterns such as these ones,

https://docs.computecanada.ca/mediawiki/index.php?title=GNU_...

Using xargs for this kind of work is euhm... not a good idea.

Re:An Opinionated Guide to Xargs by lacksconfidence on Saturday August 21, 2021 @01:29PM (#28258818)
i don't know if xargs cant, but i use gnu parallel to split an input pipe into N parallel pipes processing slices of the input stream.

Edit: To clarify, xargs usually wants to spin up a process per task. I have parallel spin up N processes and then continuously feed them.

Re:An Opinionated Guide to Xargs by bsmithers on Saturday August 21, 2021 @02:08PM (#28259155)
Not to be pedantic, but that's a bit of a non-argument. _Of course_ you can do it with xargs and shell, but imho parallel is generally more convenient, especially for remote execution. It provides a higher level of abstraction for such tasks.
Re:An Opinionated Guide to Xargs by enriquto on Sunday August 22, 2021 @03:22AM (#28263571)
> What does it do that xargs and shell can't? (honest question)

For me, an essential feature of GNU parallel is that it is semantically equivalent to "sh". Imagine that you write a file that contains a long list of commands. You can pipe that file to "sh" to run the commands, or pipe it to "parallel" to do the same, but faster. If you are building the list of commands on the fly, then you can use xargs with a slightly different syntax. But somehow using "sh" or "parallel" gives a certain peace of mind due to its straightforward semantics. I never used any argument of GNU parallel apart from -j

My usage pattern: to build a list of commands explicitly then run it (possibly teeing the list into a temporary file to inspect it):

    for i in one two three; do
            printf "echo $i\n"   
    done |sh  # or |parallel
Re:An Opinionated Guide to Xargs by oofabz on Sunday August 22, 2021 @12:31PM (#28266854)
I use GNU Parallel for long-running jobs for its --eta option. If a job will take days or longer it's useful to know that early in the process. You might want to cancel it and try something else, and if you want to proceed with the long job you can make plans around when your data will be ready.
Re:An Opinionated Guide to Xargs by MainJane on Saturday August 21, 2021 @09:31PM (#28262135)
It is documented in the GNU Parallel documentation: https://www.gnu.org/software/parallel/parallel_alternatives....

If you seriously believe you can implement everything using xargs, then this (contrived) example is for you: https://unix.stackexchange.com/questions/405552/using-xargs-...

Newer versions include 'parset' which can set shell variables in parallel, which is useful if you want to 'map' values from one array to another.

Re:An Opinionated Guide to Xargs by bloopernova on Saturday August 21, 2021 @02:34PM (#28259339)
GNU Parallel can be sourced into a bash session from a plain text file and used as a function. I've used it to get around overly-restrictive build environments. (overly restrictive because the team that manages the build image wasn't open to modifying their image for my use case)
Re:An Opinionated Guide to Xargs by orf on Saturday August 21, 2021 @01:36PM (#28258886)
Resumption, error reporting and much better progress monitoring.
Re:An Opinionated Guide to Xargs by vhold on Saturday August 21, 2021 @01:49PM (#28258979)
Oh I didn't know about resumption.. parallel has so many features packed into its CLI it's kind of ridiculous.

For others that didn't know about it, see the examples here: https://www.gnu.org/software/parallel/parallel_tutorial.html...

Here's another surprising feature: https://www.gnu.org/software/parallel/parallel_tutorial.html...

Re:An Opinionated Guide to Xargs by O_H_E on Saturday August 21, 2021 @10:14PM (#28262297)
Wow!, That is surprising and potentially very useful.
Re:An Opinionated Guide to Xargs by leephillips on Saturday August 21, 2021 @01:46PM (#28258950)
Remote execution.
Re:An Opinionated Guide to Xargs by chubot on Saturday August 21, 2021 @01:50PM (#28258993)
I'd like to see a demo of it! I will try rewriting it with the $0 Dispatch Pattern and ssh :)
Re:An Opinionated Guide to Xargs by xmcqdpt2 on Saturday August 21, 2021 @02:38PM (#28259368)
Good luck balancing node usage!

Here is an example of how it works,

https://docs.computecanada.ca/mediawiki/index.php?title=GNU_...

This + restart capabilities make gnu parallel very well suited to running 1000s of compute-heavy jobs on HPC clusters.

Re:An Opinionated Guide to Xargs by figomore on Saturday August 21, 2021 @03:06PM (#28259607)
I used Parallel to distribute the rendering of a little Blender animation It worked very well.

https://github.com/tfmoraes/blender_gnu_parallel_render/blob...

Re:An Opinionated Guide to Xargs by orhmeh09 on Saturday August 21, 2021 @03:15PM (#28259673)
Issue complaint prompts to promote the author, for one.