zsh-users
 help / color / mirror / code / Atom feed
* Is this possible in ZSH?
@ 2010-11-14 13:12 nix
  2010-11-14 20:10 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: nix @ 2010-11-14 13:12 UTC (permalink / raw)
  To: zsh-users

Hi.
#!/bin/zsh

[CODE]

zmodload zsh/terminfo
emulate zsh

function Echo () {

# This function simulates background process that executes in a random period

#local A
#local B

A=$1
B=$2

sleep $B

echo "Key: $A Value: $B"
}

A=0
RANGE=15

while [ $A -lt 4 ] ; do

Rnd=$RANDOM
let "Rnd %= $RANGE"

# - Fork

Echo $A $Rnd & # <-----------
let "A++"

done

wait

[/CODE]

Is it possible to append background process results to a variable or to an
array without waiting for that job to finish? So as soon as "Echo"
function finishes, it´s results would be appended instead of file to
variable. I have tried it but every time it waited for the process until
it finished and i lost my multi-threading.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Is this possible in ZSH?
  2010-11-14 13:12 Is this possible in ZSH? nix
@ 2010-11-14 20:10 ` Bart Schaefer
  2010-11-14 22:01   ` nix
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2010-11-14 20:10 UTC (permalink / raw)
  To: zsh-users

On Nov 14,  3:12pm, nix@myproxylists.com wrote:
}
} Is it possible to append background process results to a variable or
} to an array without waiting for that job to finish?

You're effectively asking whether the shell implements multiprocess
shared memory, because otherwise "process results" are only available
as either pipe I/O or as the exit status of the job.  The short answer
is no, you can't declare a shell variable that shares writable memory
with a forked child.

There are several alternatives to simulate the effect you want.  You
can use either coprocesses (several examples are in the mailing list
archives, search for "coproc") or the zsh/net/socket module to set up
multiple simultaneous connections from background jobs to the shell.
You can use a trap on SIGCHLD or a loop on zselect (the zsh/zselect
module) to pick up the data as the jobs exit.

Or you can use the zsh/mapfile module to simulate a shared variable.
Compare:

x=32
repeat 3 do (let "x /= 2"); print $x; done
unset x

zmodload zsh/mapfile
mapfile[shared]=32
repeat 3 do (let "mapfile[shared] /= 2"); print $mapfile[shared]; done
unset 'mapfile[shared]'

Just be sure the name you use in place of "shared" is something unique,
because you will destroy the contents of any file in $PWD that has that
name.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Is this possible in ZSH?
  2010-11-14 20:10 ` Bart Schaefer
@ 2010-11-14 22:01   ` nix
  0 siblings, 0 replies; 3+ messages in thread
From: nix @ 2010-11-14 22:01 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

> On Nov 14,  3:12pm, nix@myproxylists.com wrote:
> }
> } Is it possible to append background process results to a variable or
> } to an array without waiting for that job to finish?
>
> You're effectively asking whether the shell implements multiprocess
> shared memory, because otherwise "process results" are only available
> as either pipe I/O or as the exit status of the job.  The short answer
> is no, you can't declare a shell variable that shares writable memory
> with a forked child.
>
> There are several alternatives to simulate the effect you want.  You
> can use either coprocesses (several examples are in the mailing list
> archives, search for "coproc") or the zsh/net/socket module to set up
> multiple simultaneous connections from background jobs to the shell.
> You can use a trap on SIGCHLD or a loop on zselect (the zsh/zselect
> module) to pick up the data as the jobs exit.
>
> Or you can use the zsh/mapfile module to simulate a shared variable.
> Compare:
>
> x=32
> repeat 3 do (let "x /= 2"); print $x; done
> unset x
>
> zmodload zsh/mapfile
> mapfile[shared]=32
> repeat 3 do (let "mapfile[shared] /= 2"); print $mapfile[shared]; done
> unset 'mapfile[shared]'
>
> Just be sure the name you use in place of "shared" is something unique,
> because you will destroy the contents of any file in $PWD that has that
> name.
>

Thanks for this hint, i try to reproduce something based on your help.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-11-14 22:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-14 13:12 Is this possible in ZSH? nix
2010-11-14 20:10 ` Bart Schaefer
2010-11-14 22:01   ` nix

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).