If you want to execute a command in the background in Tcl, you could go for something along the line of:
proc cb { fd } {
gets $fd buf
append ::output $buf
if {[eof $fd]} {
close $fd
set ::finished 1
}
}
set command "<command to execute>"
set ::output ""
set ::finished 0
set fd [open "|$command" r]
fconfigure $fd -blocking no
fileevent $fd readable "cb $fd"
vwait ::finished
puts $::output
The use of open
with a |
before the command will allow you to "open" a pipe to the command. Using fconfigure
to set it non-blocking will allow you to read from it without locking any other process in the script. fileevent
will call the specified callback proc (in this case cb
) whenever there's data to be read (thus the readable
flag). vwait
will make sure the script does not proceed until the specified variable is written to, thus the $command
will be executed in the background, allowing say a Tk interface to remain responsive and waiting until you want to continue.
wait
at all... – Mallik