On Tuesday 03 November 2009 17:34:57 Baptiste Daroussin wrote:
> Hi,
>
> I wanted to write a script that needs to be multithreaded, for example
> an UI in zsh/curses that does things in background while the UI is
> still responsive.
It's possible to do background tasks in subshells. Simple append an & to
the end of the subshell list:
print "hi_0"; { print "hi_1"; sleep 60; print "hi_2"} & print "hi_3"; \
sleep 20; print "hi_4"
Somewhere on the internet there is an example in bash of how to setup a
multiprocess shell program which uses subshells as worker processes. The
communication between these processes is done via named pipes. It's much
like Apache's worker process model.
Here is a simple and quick suggestion how such an architecture may look
like in zsh:
#!/bin/zsh
worker() {
while (true) {
print "job started: $1"
read -u 0
eval "$REPLY"
print "job done: $1"
}
}
mkfifo pipe
worker "worker 0" < pipe & workers+=($!)
worker "worker 1" < pipe & workers+=($!)
print "sleep 20" >> pipe
print "sleep 30" >> pipe
print "sleep 5; ls; sleep 5" >> pipe
sleep 20
print "sleep 20" >> pipe
print "exit\nexit" >> pipe
wait ${workers[*]}
rm pipe
exit 0
Attachment:
signature.asc
Description: This is a digitally signed message part.