zsh-users
 help / color / mirror / code / Atom feed
From: Philippe Troin <phil@fifi.org>
To: Bart Schaefer <schaefer@brasslantern.com>,
	Perry Smith <pedz@easesoftware.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Parallel processing
Date: Sat, 26 Mar 2022 11:10:37 -0700	[thread overview]
Message-ID: <36966db7bf519a888d7daca39fdd39f1e39b8511.camel@fifi.org> (raw)
In-Reply-To: <CAH+w=7baN47QxGiga9WVTHHny7JnjbbCudfkDKR+qEq1pAgcnw@mail.gmail.com>

On Fri, 2022-03-25 at 11:27 -0700, Bart Schaefer wrote:
> On Thu, Mar 24, 2022 at 9:34 PM Perry Smith <pedz@easesoftware.com> wrote:
> 
> 
> This isn't exactly what you want because it waits for all four jobs
> before starting the next batch, but keeping a specific number of
> children running is not straightforward with the job-management
> operations available to a shell.

There may be a way to achieve keeping a set number of children around,
by trapping SIGCHLD, but we would completely lose the exit status of
the command.  There does not seem to be a way to retrieve the exit
status of a command as soon as SIGCHLD is trapped:

   % zsh -f
   % echo $ZSH_VERSION
   5.8.1
   
   % setopt monitor
   % trap 'x=$?; echo "CHLD args=$* exit=$x"; wait $PID ' CHLD; (sleep 1;  exit 1) & PID=$!; wait; echo "wait: $?"
   [1] 1192215
   [1]  + exit 1     ( sleep 1; exit 1; )
   CHLD args= exit=0
   wait: pid 1192215 is not a child of this shell
   wait: 0
   
   % setopt nomonitor
   % trap 'x=$?; echo "CHLD args=$* exit=$x"; wait $PID ' CHLD; (sleep 1; exit 1) & PID=$!; wait; echo "wait: $?"
   CHLD args= exit=0
   wait: pid 1192528 is not a child of this shell
   wait: 0
   
   % trap - SIGCHLD
   % TRAPCHLD()  { echo "CHLD args=$*"; wait $PID }; (sleep 1; exit 1) & PID=$!; wait; echo "wait: $?"
   CHLD args=17
   TRAPCHLD:wait: pid 1192701 is not a child of this shell
   wait: 0

Collecting background jobs' exit status is discussed in the manual,
under the POSIX_JOBS option:

   In  previous  versions  of the shell, it was necessary to enable
   POSIX_JOBS in order for the builtin command wait to  return  the
   status of  background jobs that had already exited.  This is no
   longer the case.

Setting/unsetting POSIX_JOBS does not make any difference.

Anyways, zargs is not doing a stellar job currently with collecting
exit statuses from commands ran in parallel:

   % zsh -f
   % autoload zargs
   % zargs -n 4 -P 2 -- 1 0 -- zsh -c 'sleep $1 ; exit $1 ' -; echo $?
   0
   % zargs -n 4 -P 2 -- 0 1 -- zsh -c 'sleep $1 ; exit $1 ' -; echo $?
   123
   % zargs -n 2 -P 2 -- 1 0 -- eval '(){ sleep $1 ; return $1 }' ; echo $?
   0
   % zargs -n 2 -P 2 -- 0 1 -- eval '(){ sleep $1 ; return $1 }' ; echo $? 
   123

Phil.


  reply	other threads:[~2022-03-26 18:11 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-25  4:34 Perry Smith
2022-03-25 18:27 ` Bart Schaefer
2022-03-26 18:10   ` Philippe Troin [this message]
2022-03-26 22:19     ` Bart Schaefer
2022-03-27 17:32       ` Philippe Troin
2022-03-27 17:42         ` Bart Schaefer
2022-03-27 19:23 ` Dominik Vogt
2022-03-28 13:26   ` Perry Smith
2022-03-28 13:31 ` Perry Smith
2022-03-26  4:34 jdh
2022-03-27 18:37 jdh
2022-03-27 19:06 ` Bart Schaefer
2022-03-27 19:24   ` Ray Andrews
2022-03-28 13:47 ` Perry Smith

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=36966db7bf519a888d7daca39fdd39f1e39b8511.camel@fifi.org \
    --to=phil@fifi.org \
    --cc=pedz@easesoftware.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    --subject='Re: Parallel processing' \
    /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

Code repositories for project(s) associated with this 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).