| 1 |
<!-- manual page source format generated by PolyglotMan v3.0.8+XFree86, --> |
| 2 |
<!-- available via anonymous ftp from ftp.cs.berkeley.edu:/ucb/people/phelps/tcltk/rman.tar.Z --> |
| 3 |
|
| 4 |
<HTML> |
| 5 |
<HEAD> |
| 6 |
<TITLE>bgexec(n) manual page</TITLE> |
| 7 |
</HEAD> |
| 8 |
<BODY BGCOLOR="#efefef" TEXT="black" LINK="blue" VLINK="#551A8B" ALINK="red"> |
| 9 |
<A HREF="#toc">Table of Contents</A><P> |
| 10 |
|
| 11 |
<H2><A NAME="sect0" HREF="#toc0">Name</A></H2> |
| 12 |
bgexec - Run programs in the background while |
| 13 |
handling Tk events. |
| 14 |
<H2><A NAME="sect1" HREF="#toc1">Synopsis</A></H2> |
| 15 |
<B>blt::bgexec <I>varName</I></B> ?<I>option value</I>?... <I>program</I> ?<I>arg</I>?... |
| 16 |
|
| 17 |
<H2><A NAME="sect2" HREF="#toc2">Description</A></H2> |
| 18 |
<P> |
| 19 |
The <B>bgexec</B> command executes programs in the background, allowing |
| 20 |
Tk to handle events. A global Tcl variable <I>varName</I> is set when the program |
| 21 |
has completed. |
| 22 |
<H2><A NAME="sect3" HREF="#toc3">Introduction</A></H2> |
| 23 |
Tcl's <B>exec</B> command is very useful for gathering |
| 24 |
information from the operating system. It runs a program and returns the |
| 25 |
output as its result. This works well for Tcl-only applications. But for |
| 26 |
Tk applications, a problem occurs when the program takes time to process. |
| 27 |
Let's say we want the get the disk usage of a directory. We'll use the Unix |
| 28 |
program <I>du</I> to get the summary. <BR> |
| 29 |
<CODE>set out [exec du -s $dir]<BR> |
| 30 |
puts "Disk usage for $dir is $out"<BR> |
| 31 |
</CODE><P>While <I>du</I> is running, scrollbars won't respond. None of the Tk widgets will |
| 32 |
be redrawn properly. The <B>send</B> command won't work. And the worst part is that |
| 33 |
the application appears hung up or dead. The problem is that while <B>exec</B> |
| 34 |
is waiting for <I>du</I> to finish, Tk is not able to handle X events. <P> |
| 35 |
The <B>bgexec</B> |
| 36 |
command performs the same functions as <B>exec</B>, but also allows Tk to handle |
| 37 |
events. You can execute a long-running program and the Tk widgets will behave |
| 38 |
normally. When the program finishes, its output and the exit status are |
| 39 |
written to Tcl variables. This makes it easy to monitor and save the output |
| 40 |
of a program. |
| 41 |
<H2><A NAME="sect4" HREF="#toc4">Example</A></H2> |
| 42 |
Here is the disk usage example again, this time using |
| 43 |
<B>bgexec</B>. The syntax to invoke "du" is exactly the same as the previous example, |
| 44 |
when we used <B>exec</B>. <BR> |
| 45 |
<CODE>global myStatus myOutput<BR> |
| 46 |
blt::bgexec myStatus -output myOutput du -s $dir<BR> |
| 47 |
puts "Disk usage for $dir is $myOutput"<BR> |
| 48 |
</CODE><P>Two global variables, <I>myStatus</I> and <I>myOutput</I>, will be set by <B>bgexec</B> when |
| 49 |
<I>du</I> has completed. <I>MyStatus</I> will contain the program's exit status. <I>MyOutput</I>, |
| 50 |
specified by the <B>-output</B> option, will store the output of the program. <P> |
| 51 |
You |
| 52 |
can also terminate the program by setting the variable <I>myStatus</I>. If <I>myStatus</I> |
| 53 |
is set before <I>du</I> has completed, the process is killed. Under Unix, this |
| 54 |
is done sending by a configurable signal (by default it's SIGKILL). Under |
| 55 |
Win32, this is done by calling <B>TerminateProcess</B>. It makes no difference |
| 56 |
what <I>myStatus</I> is set to. <BR> |
| 57 |
<CODE>set myStatus {}<BR> |
| 58 |
</CODE><P>There are several <B>bgexec</B> options to collect different types of information. |
| 59 |
<BR> |
| 60 |
<CODE>global myStatus myOutput myErrs<BR> |
| 61 |
blt::bgexec myStatus -output myOutput -error myErrs du -s $dir<BR> |
| 62 |
</CODE><P>The <B>-error</B> option is similar to <B>-output</B>. It sets a global variable when the |
| 63 |
program completes. The variable will contain any data written to stderr |
| 64 |
by the program. <P> |
| 65 |
The <B>-output</B> and <B>-error</B> variables are set only after the program |
| 66 |
completes. But if the program takes a long time, to run you may want to |
| 67 |
receive its partial output. You can gather data as it becomes available |
| 68 |
using the <B>-onoutput</B> option. It specifies a Tcl command prefix. Whenever |
| 69 |
new data is available, this command is executed, with the data appended |
| 70 |
as an argument to the command. <BR> |
| 71 |
<CODE>proc GetInfo { data } {<BR> |
| 72 |
puts $data<BR> |
| 73 |
}<BR> |
| 74 |
blt::bgexec myStatus -onoutput GetInfo du -s $dir<BR> |
| 75 |
</CODE><P>When output is available, the procedure <I>GetInfo</I> is called. The <B>-onerror</B> option |
| 76 |
performs a similar function for the stderr data stream. <P> |
| 77 |
Like <B>exec</B>, <B>bgexec</B> |
| 78 |
returns an error if the exit code of the program is not zero. If you think |
| 79 |
you may get a non-zero exit code, you might want to invoke <B>bgexec</B> from within |
| 80 |
a <B>catch</B>. <BR> |
| 81 |
<CODE>catch { blt::bgexec myStatus -output myOutput du -s $dir }<BR> |
| 82 |
</CODE><P>By default, <B>bgexec</B> will wait for the program to finish. But you can detach |
| 83 |
the program making ampersand (&) the last argument on the command line. <BR> |
| 84 |
<CODE>global myStatus myOutput<BR> |
| 85 |
blt::bgexec myStatus -output myOutput du -s $dir &<BR> |
| 86 |
</CODE><P><B>Bgexec</B> will return immediately and its result will be a list of the spawned |
| 87 |
process ids. If at some point you need to wait for the program to finish |
| 88 |
up, you can use <B>tkwait</B>. When the program finishes, the variable <I>myStatus</I> |
| 89 |
will be written to, breaking out the <B>tkwait</B> command. <BR> |
| 90 |
<CODE>global myStatus myOutput<BR> |
| 91 |
blt::bgexec myStatus -output myOutput du -s $dir &<BR> |
| 92 |
<tt> </tt> <tt> </tt> ...<BR> |
| 93 |
tkwait variable myStatus<BR> |
| 94 |
|
| 95 |
<H2><A NAME="sect5" HREF="#toc5"></CODE><P>Syntax</A></H2> |
| 96 |
The <B>bgexec</B> command takes the following form: <P> |
| 97 |
<B><tt> </tt> <tt> </tt> blt::bgexec <I>varName</I></B> |
| 98 |
?<I>option value</I>?... <I>program</I> ?<I>arg</I>?... <P> |
| 99 |
<I>VarName</I> is the name of a global variable which |
| 100 |
is set when <I>program</I> has finished executing. The exit status of will be |
| 101 |
stored in <I>varName</I>. The exit status is a list of a status token, the process-id |
| 102 |
of the program, the exit code, and a status message. You can also prematurely |
| 103 |
terminate the program by setting <I>varName</I>. Under Unix, the program will |
| 104 |
be sent a signal to terminate it (by default the signal is a SIGKILL; see |
| 105 |
the <B>-killsignal</B> option). <P> |
| 106 |
<I>Program</I> is the name of the program to be executed |
| 107 |
and <I>args</I> are any extra arguments for <I>program</I>. The syntax of <I>program</I> and |
| 108 |
<I>args</I> is the same as the <B>exec</B> command. So you can redirect I/O, execute pipelines, |
| 109 |
etc. (see the <B>exec</B> manual for further information) just like <B>exec</B>. If the |
| 110 |
last argument is an ampersand (&), the program will be run detached, and |
| 111 |
<B>bgexec</B> will return immediately. <I>VarName</I> will still be set with the return |
| 112 |
status when <I>program</I> completes. |
| 113 |
<H2><A NAME="sect6" HREF="#toc6">Options</A></H2> |
| 114 |
<I>Option</I> refers to the switch name always |
| 115 |
beginning with a dash (-). <I>Value</I> is the value of the option. Option-value |
| 116 |
pairs are terminated either by the program name, or double dashes (--). The |
| 117 |
following options are available for <B>bgexec</B>: |
| 118 |
<DL> |
| 119 |
|
| 120 |
<DT><B>-decodeerror <I>encodingName</I></B> </DT> |
| 121 |
<DD><BR> |
| 122 |
Specifies the encoding of the stderr channel. This affects only data returned |
| 123 |
to the Tcl interpreter. No translation is done on file redirection. <BR> |
| 124 |
For example if data is to be converted from Unicode for use in Tcl, you |
| 125 |
would use the "unicode" encoding. The default is that no tranlation is |
| 126 |
performed. </DD> |
| 127 |
|
| 128 |
<DT><B>-decodeoutput <I>encodingName</I></B> </DT> |
| 129 |
<DD><BR> |
| 130 |
Specifies the encoding of the stdout channels. This affects only data returned |
| 131 |
to the Tcl interpreter. No translation is done on file redirection. <BR> |
| 132 |
For example if data is to be converted from Unicode for use in Tcl, you |
| 133 |
would use the "unicode" encoding. The default is that no tranlation is |
| 134 |
performed. </DD> |
| 135 |
|
| 136 |
<DT><B>-error <I>varName</I></B> </DT> |
| 137 |
<DD><BR> |
| 138 |
Specifies that a global variable <I>varName</I> is to be set with the contents |
| 139 |
of stderr after the program has completed. </DD> |
| 140 |
|
| 141 |
<DT><B>-keepnewline <I>boolean</I></B> </DT> |
| 142 |
<DD>Specifies |
| 143 |
that a trailing newline should be retained in the output. If <I>boolean</I> is |
| 144 |
true, the trailing newline is truncated from the output of the <B>-onoutput</B> |
| 145 |
and <B>-output</B> variables. The default value is <I>true</I>. </DD> |
| 146 |
|
| 147 |
<DT><B>-killsignal <I>signal</I></B> </DT> |
| 148 |
<DD>Specifies |
| 149 |
the signal to be sent to the program when terminating. This is available |
| 150 |
only under Unix. <I>Signal</I> can either be a number (typically 1-32) or a mnemonic |
| 151 |
(such as SIGINT). If <I>signal</I> is the empty string, then no signal is sent. |
| 152 |
The default signal is <I>9</I> (SIGKILL). </DD> |
| 153 |
|
| 154 |
<DT><B>-lasterror <I>varName</I></B> </DT> |
| 155 |
<DD>Specifies a variable |
| 156 |
<I>varName</I> that is updated whenever data becomes available from standard error |
| 157 |
of the program. <I>VarName</I> is a global variable. Unlike the <B>-error</B> option, data |
| 158 |
is available as soon as it arrives. </DD> |
| 159 |
|
| 160 |
<DT><B>-lastoutput <I>varName</I></B> </DT> |
| 161 |
<DD>Specifies a variable |
| 162 |
<I>varName</I> that is updated whenever data becomes available from standard output |
| 163 |
of the program. <I>VarName</I> is a global variable. Unlike the <B>-output</B> option, data |
| 164 |
is available as soon as it arrives. </DD> |
| 165 |
|
| 166 |
<DT><B>-linebuffered <I>boolean</I></B> </DT> |
| 167 |
<DD>Specifies that |
| 168 |
updates should be made on a line-by-line basis. Normally when new data is |
| 169 |
available <B>bgexec</B> will set the variable (<B>-lastoutput</B> and <B>-lasterror</B> options) |
| 170 |
or invoke the command (<B>-onoutput</B> and <B>-onerror</B> options) delivering all the |
| 171 |
new data currently available. If <I>boolean</I> is true, only one line at a time |
| 172 |
will be delivered. This can be useful when you want to process the output |
| 173 |
on a line-by-line basis. The default value is <I>false</I>. </DD> |
| 174 |
|
| 175 |
<DT><B>-output <I>varName</I></B> </DT> |
| 176 |
<DD><BR> |
| 177 |
Specifies that a global variable <I>varName</I> is to be set with the output of |
| 178 |
the program, once it has completed. If this option is not set, no output |
| 179 |
will be accumulated. </DD> |
| 180 |
|
| 181 |
<DT><B>-onerror <I>command</I></B> </DT> |
| 182 |
<DD>Specifies the start of a Tcl command |
| 183 |
that will be executed whenever new data is available from standard error. |
| 184 |
The data is appended to the command as an extra argument before it is executed. |
| 185 |
</DD> |
| 186 |
|
| 187 |
<DT><B>-onoutput <I>command</I></B> </DT> |
| 188 |
<DD>Specifies the start of a Tcl command that will be executed |
| 189 |
whenever new data is available from standard output. The data is appended |
| 190 |
to the command as an extra argument before it is executed. </DD> |
| 191 |
|
| 192 |
<DT><B>-update <I>varName</I></B> |
| 193 |
</DT> |
| 194 |
<DD>Deprecated. This option is replaced by <B>-lasterror</B>. </DD> |
| 195 |
|
| 196 |
<DT><B>--</B> </DT> |
| 197 |
<DD>This marks the end of |
| 198 |
the options. The following argument will be considered the name of a program |
| 199 |
even if it starts with a dash (<B>-</B>). </DD> |
| 200 |
</DL> |
| 201 |
|
| 202 |
<H2><A NAME="sect7" HREF="#toc7">Preemption</A></H2> |
| 203 |
Because <B>bgexec</B> allows Tk to |
| 204 |
handle events while a program is running, it's possible for an application |
| 205 |
to preempt itself with further user-interactions. Let's say your application |
| 206 |
has a button that runs the disk usage example. And while the <I>du</I> program |
| 207 |
is running, the user accidently presses the button again. A second <B>bgexec</B> |
| 208 |
program will preempt the first. What this means is that the first program |
| 209 |
can not finish until the second program has completed. <P> |
| 210 |
Care must be taken |
| 211 |
to prevent an application from preempting itself by blocking further user-interactions |
| 212 |
(such as button clicks). The BLT <B>busy</B> command is very useful for just these |
| 213 |
situations. See the <B>busy</B> manual for details. |
| 214 |
<H2><A NAME="sect8" HREF="#toc8">Differences with Fileevent</A></H2> |
| 215 |
Since |
| 216 |
Tk 4.0, a subset of <B>bgexec</B> can be also achieved using the <B>fileevent</B> command. |
| 217 |
The steps for running a program in the background are: <P> |
| 218 |
Execute the program |
| 219 |
with the <B>open</B> command (using the "|" syntax) and save the file handle. <BR> |
| 220 |
<CODE>global fileId <BR> |
| 221 |
set fileId [open "|du -s $dir" r]<BR> |
| 222 |
</CODE><P>Next register a Tcl code snippet with <B>fileevent</B> to be run whenever output |
| 223 |
is available on the file handle. The code snippet will read from the file |
| 224 |
handle and save the output in a variable. <BR> |
| 225 |
<CODE>fileevent fileId readable { <BR> |
| 226 |
if { [gets $fileId line] < 0 } {<BR> |
| 227 |
<tt> </tt> <tt> </tt> close $fileId<BR> |
| 228 |
<tt> </tt> <tt> </tt> set output $temp<BR> |
| 229 |
<tt> </tt> <tt> </tt> unset fileId temp<BR> |
| 230 |
} else {<BR> |
| 231 |
<tt> </tt> <tt> </tt> append temp $line<BR> |
| 232 |
}<BR> |
| 233 |
}<BR> |
| 234 |
<P> |
| 235 |
</CODE><P>The biggest advantage of <B>bgexec</B> is that, unlike <B>fileevent</B>, it requires |
| 236 |
no additional Tcl code to run a program. It's simpler and less error prone. |
| 237 |
You don't have to worry about non-blocking I/O. It's handled tranparently |
| 238 |
for you. <P> |
| 239 |
<B>Bgexec</B> runs programs that <B>fileevent</B> can not. <B>Fileevent</B> assumes that |
| 240 |
the when stdout is closed the program has completed. But some programs, |
| 241 |
like the Unix <I>compress</I> program, reopen stdout, fooling <B>fileevent</B> into thinking |
| 242 |
the program has terminated. In the example above, we assume that the program |
| 243 |
will write and flush its output line-by-line. However running another program, |
| 244 |
your application may block in the <B>gets</B> command reading a partial line. <P> |
| 245 |
<B>Bgexec</B> |
| 246 |
lets you get back the exit status of the program. It also allows you to |
| 247 |
collect data from both stdout and stderr simultaneously. Finally, since |
| 248 |
data collection is handled in C code, <B>bgexec</B> is faster. You get back to |
| 249 |
the Tk event loop more quickly, making your application seem more responsive. |
| 250 |
|
| 251 |
<H2><A NAME="sect9" HREF="#toc9">See Also</A></H2> |
| 252 |
busy, exec, tkwait |
| 253 |
<H2><A NAME="sect10" HREF="#toc10">Keywords</A></H2> |
| 254 |
exec, background, busy <P> |
| 255 |
|
| 256 |
<HR><P> |
| 257 |
<A NAME="toc"><B>Table of Contents</B></A><P> |
| 258 |
<UL> |
| 259 |
<LI><A NAME="toc0" HREF="#sect0">Name</A></LI> |
| 260 |
<LI><A NAME="toc1" HREF="#sect1">Synopsis</A></LI> |
| 261 |
<LI><A NAME="toc2" HREF="#sect2">Description</A></LI> |
| 262 |
<LI><A NAME="toc3" HREF="#sect3">Introduction</A></LI> |
| 263 |
<LI><A NAME="toc4" HREF="#sect4">Example</A></LI> |
| 264 |
<LI><A NAME="toc5" HREF="#sect5">Syntax</A></LI> |
| 265 |
<LI><A NAME="toc6" HREF="#sect6">Options</A></LI> |
| 266 |
<LI><A NAME="toc7" HREF="#sect7">Preemption</A></LI> |
| 267 |
<LI><A NAME="toc8" HREF="#sect8">Differences with Fileevent</A></LI> |
| 268 |
<LI><A NAME="toc9" HREF="#sect9">See Also</A></LI> |
| 269 |
<LI><A NAME="toc10" HREF="#sect10">Keywords</A></LI> |
| 270 |
</UL> |
| 271 |
</BODY></HTML> |