zsh-users
 help / color / mirror / code / Atom feed
From: Zach Riggle <zachriggle@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Efficient way to map a list of values to multiple processes, then accumulate their output
Date: Sat, 6 Nov 2021 08:40:51 -0500	[thread overview]
Message-ID: <CAMP9c5m90kMiV00A5mB3ANwFjUKZQsS2BScEJnXEmgA6rLwwyA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1269 bytes --]

Hello all!  Thanks in advance for the assistance.

Ultimately, I'd like to have some quasi-implementation of xargs -P in pure
zsh -- which maps a __function__ to a list of arguments, and returns the
results in an array or associative array.  xargs cannot be used to run
shell functions, so this is out.

I think this is a situation that Zsh is not well-suited for, but let's say
I have a list of files that I want to perform some processing on, then
gather the results when the processing has completed.

Simply backgrounding each process with & and then wait'ing on all jobs to
complete should work, in theory.  However, the output from each invocation
may be intermixed, even in the simple case of "one line of output per file"
the order of completion is non-deterministic and the output may become
garbled.

This seems something that would fit nicely with co-processes, but it
appears each zsh instance can only have one.

Another idea is to farm the output of each to a temporary file, and have an
associative array of e.g. [input]=$(mktemp) as well as [input]=job_id and
then grabbing the output as each job completes.

I expect I am not the first person to try this (equivalent to the Python
multiprocessing.map) but I figured it was worth asking!

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 1657 bytes --]

             reply	other threads:[~2021-11-06 13:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-06 13:40 Zach Riggle [this message]
2021-11-06 16:18 ` Bart Schaefer
2021-11-06 21:58   ` Zach Riggle
2021-11-06 22:55     ` Lawrence Velázquez
2021-11-08 20:27       ` Zach Riggle
2021-11-08 20:45         ` Zach Riggle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMP9c5m90kMiV00A5mB3ANwFjUKZQsS2BScEJnXEmgA6rLwwyA@mail.gmail.com \
    --to=zachriggle@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).