Starting Process-Pipelines in the backgound, collecting their output via configurable callback. Requires the [event loop] to be running. ---- '''History until 1.7''' * 1.8: fixed version check [[expr {[[info patchlevel]] >= "8.4.7"}]] which does of course not always gave the right result ;-) * 1.9: Optionally signal EOF to given eofHandler * 1.10: fixed bug: number_of_processes remain incremented even if "open |..." failed. Now incr late. * 1.11: llength instead of string length for some tests. Calling EOF-handler when processing terminates via readhandler-break. * 1.12: bugfix: preventing invalid processcounter w/timeout (I hope). Only used a few hours... * 1.13: eof handler not fired if user readhandler breaks. logik of user timeout handler now equals user read handler. ---- '''BgExec-Procedure v1.13''' ====== ################################################################################ # Modul : bgexec1.13.tcl # # Changed : 02.03.2015 # # Purpose : running processes in the background, catching their output via # # event handlers # # Author : M.Hoffmann # # Hinweise : >&@ and 2>@stdout don't work on Windows. A work around probably # # could be using a temporay file. Beginning with Tcl 8.4.7 / 8.5 # # there is another (yet undocumented) way of redirection: 2>@1. # # History : # # 19.11.03 v1.0 1st version # # 20.07.04 v1.1 callback via UPLEVEL # # 08.09.04 v1.2 using 2>@1 instead of 2>@stdout if Tcl >= 8.4.7; # # timeout-feature # # 13.10.04 v1.3 bugfix in bgExecTimeout, readHandler is interruptable # # 18.10.04 v1.4 bugfix: bgExecTimeout needs to be canceled when work is done; # # some optimizations # # 14.03.05 v1.4 comments translated to english # # 17.11.05 v1.5 If specidied, a user defined timeout handler `toExit` runs in # # case of a timeout to give chance to kill the PIDs given as # # arg. Call should be compatible (optional parameter). # # 23.11.05 v1.6 User can give additional argument to his readhandler. # # 03.07.07 v1.7 Some Simplifications (almost compatible, unless returned # # string where parsed): # # - don't catch error first then returning error to main... # # 08.10.07 v1.8 fixed buggy version check! # # 20.02.12 v1.9 Optionally signal EOF to eofHandler. # # 13.09.14 v1.10 bugfix: incr myCount later (in case of an (open)error it was # # erranously incremented yet) # # 22.02.15 v1.11 llength instead of string length for some tests. Calling EOF- # # handler when processing terminates via readhandler-break. # # 28.02.15 v1.12 bugfix: preventing invalid processcounter w/timeout (I hope). # # 02.03.15 v1.13 eof handler not fired if user readhandler breaks. # # logik of user timeout handler now equals user read handler. # # ATTENTION: closing a pipe leads to error broken pipe if the opened process # # itself is a tclsh interpreter. Currently I don't know how to # # avoid this without killing the process via toExit before closing # # the pipeline. # # - This Code uses one global var, the counter of currently started pipelines. # # TODO: (generic) Errorhandler? Namespace or OO to clean up overall design. # ################################################################################ # ATTENTION: This is the last version which maintains upward compatibility (I hope) package provide bgexec 1.13 #------------------------------------------------------------------------------- # If the ram successfully starts, its STDOUT and STDERR is dispatched # line by line to the (via bgExecGenericHandler) as last arg. The # global var holds the number of processes called this way. If a # is specified (as msecs), the process pipeline will be automatically closed after # that duration. If specified, and a timeout occurs, is called with the # PIDs of the processes right before closing the process pipeline. # Returns the handle of the process-pipeline. # proc bgExec {prog readHandler pCount {timeout 0} {toExit ""} {eofHandler ""}} { upvar #0 $pCount myCount set p [expr {[lindex [lsort -dict [list 8.4.7 [info patchlevel]]] 0] == "8.4.7"?"| $prog 2>@1":"| $prog 2>@stdout"}] set pH [open $p r] # Possible Problem if both after event and fileevents are delayed (no event loop) until timeout fires; # ProcessCount is then decremented before ever incremented. So increment ProcessCount early! set myCount [expr {[info exists myCount]?[incr myCount]:1}]; # precaution < 8.6 fconfigure $pH -blocking 0; # -buffering line (does it really matter?!) set tID [expr {$timeout?[after $timeout [list bgExecTimeout $pH $pCount $toExit]]:{}}] fileevent $pH readable [list bgExecGenericHandler $pH $pCount $readHandler $tID $eofHandler] return $pH } #------------------------------------------------------------------------------- proc bgExecGenericHandler {chan pCount readHandler tID eofHandler} { upvar #0 $pCount myCount if {[eof $chan]} { after cancel $tID; # terminate Timeout, no longer needed! catch {close $chan}; # automatically deregisters the fileevent handler incr myCount -1 if {[llength $eofHandler]} { catch {uplevel $eofHandler $chan}; # not called on timeout or user-break } } elseif {[gets $chan line] != -1} { # we are not blocked lappend readHandler $line if {[catch {uplevel $readHandler}]} { # user-readHandler ended with errorcode which means here # "terminate the processing". NOTE eof-handler NOT fired! after cancel $tID catch {close $chan} incr myCount -1 } } } #------------------------------------------------------------------------------- proc bgExecTimeout {chan pCount toExit} { upvar #0 $pCount myCount if {[string length $toExit]} { # The PIDs are one arg (list) if {[catch {uplevel [list {*}$toExit [pid $chan]]}]} { # user-timeoutHandler ended with error which means here # "we didn't kill the processes" (such a kill would have # normally triggered an EOF, so no other cleanup would be # required then), so end the processing explicitely and do # the cleanup. NOTE eof-handler NOT fired! catch {close $chan} incr myCount -1 } } else { # No user-timeoutHandler exists. # So we must cleanup anyway (at least some level of compatibility...) # NOTE eof-handler NOT fired! catch {close $chan} incr myCount -1 } } #=============================================================================== ====== ---- '''So, what is this program for at all?''' * '''keep the user interface responding'''! * parallelizing processes A user interface could be * A long running command line procedure * A long running CGI proc * A Tk interface If you simply [exec] a long running external program, waiting around for it's output, the user interface will be blocked meanwhile. If you start it in the background with '''&''' and some ''redirection'', you can continue with your process, but you have to detect and wait until the external program finishes before you can open the wanted file(s) with the program feedback (output). It would be better if the user begins receiving feedback as soon as possible, especially with webservers/cgi-scripts - that's what I wrote this module for originally. You quickly see the first output record of the called program. After all it's just a wrapper for [open] |prog and [fileevent]... However, there are some performance drawbacks driving programs this way. '''Remarks''' * Due to TCL's lack of process killing cababilities, it's likely that processes continue to run in some state where they lost their stdout/stderr-channels after a timeout arised. Closing the TCL-channels doesn't seem to help. So it is better to additionally use an external process killer (many of them are available on windows). The "kill" subcommand of the TclX package works fine for this. With version 1.5, this can be triggered automatically via the new UserExit ''toExit'' , so the user can decide which process killer to use. * As of tcl 8.4.11.2, the '''2>@1''' still seems to be undocumented.... As of Tcl 8.4.11.2, '''2>@1''' is still undocumented.... (still seeing and hoping) ---- ''Remark: The following procs are not always up to date''' '''Testproc bgexec1.6_test.tcl''' # Testing and demonstrating bgExec v1.6 # 23.11.2005 lappend auto_path . package require bgexec 1.6 proc dummy {userdata what} { # data from delayout1 & 3 puts >>>$what<<<$userdata } proc dummy2 {userdata what} { # data from delayout2 puts >>>$what<<<$userdata return -code error {Break triggerd via ReadHandler} } proc time {} { puts [clock format [clock seconds]] after 1000 [list time] } proc toExit {PIDs} { puts "Timeoutexit: $PIDs; trying to kill process" # avoid 'broken pipe' foreach PID $PIDs { # for PV, see http://www.xmlsp.com/pview/prcview.htm catch {exec -- [auto_execok pv] -k -f -i $PID} rc puts $rc } } after 1000 [list time] set h1 [bgExec "[info nameofexe] delayout1.tcl" [list dummy *1*] pCount] puts "Handle: $h1" catch {puts [pid $h1]} set h2 [bgExec "[info nameofexe] delayout2.tcl" [list dummy2 *2*] pCount] puts "Handle: $h2" catch {puts [pid $h2]} set h3 [bgExec "[info nameofexe] delayout3.tcl" [list dummy *3*] pCount 5000 toExit] puts "Handle: $h3" catch {puts [pid $h3]} puts "pCount: $pCount" # alternative: vwait pCount (problematic as pCount has to be GLOBAL) while {$pCount > 0} { vwait pCount puts "pCount: $pCount" # or: update; # not: update idletasks! } puts "pCount (after loop): $pCount" '''And the three-Testsubprocs:''' '''''delayout1.tcl:''''' puts "1 output to STDOUT" puts "1 output to STDOUT" puts "1 output to STDOUT" puts stderr "1 output to STDERR" puts stderr "1 output to STDERR" puts stderr "1 output to STDERR - a normal end via EOF" '''''delayout2.tcl:''''' puts "2 output to STDOUT - aborts after this, via UserReadHandler" puts "2 output to STDOUT" puts "2 output to STDOUT" puts stderr "2 output to STDERR" puts stderr "2 output to STDERR" puts stderr "2 output to STDERR" '''''delayout3.tcl:''''' puts "3 output to STDOUT" puts "3 output to STDOUT" puts "3 output to STDOUT" puts stderr "3 output to STDERR" puts stderr "3 output to STDERR" puts stderr "3 output to STDERR" after 3000 puts "3 output to STDOUT" puts "3 output to STDOUT" puts "3 output to STDOUT" puts stderr "3 output to STDERR" puts stderr "3 output to STDERR" puts stderr "3 output to STDERR" after 3000 puts "3 output to STDOUT" puts "3 output to STDOUT" puts "3 output to STDOUT" puts stderr "3 output to STDERR" puts stderr "3 output to STDERR" puts stderr "3 output to STDERR - this is not displayed anymore - script's ending somewhere before after 5000s via timeout" '''To test everything:''' * do '''tclsh bgexec1.6_test.tcl''' Output should appear similar to the following: Handle: file798570 1336 Handle: file841908 1368 2 Output To STDERR 2 Output To STDERR 2 Output To STDERR Handle: file840d08 1380 pCount: 3 >>>2 Output To STDOUT - should be the last one due to Break via UserReadHandler< <<*2* pCount: 2 >>>1 Output To STDOUT<<<*1* >>>1 Output To STDOUT<<<*1* >>>1 Output To STDOUT<<<*1* 1 Output To STDERR 1 Output To STDERR 1 Output To STDERR - should normally end via EOF pCount: 1 >>>3 Output To STDOUT<<<*3* >>>3 Output To STDOUT<<<*3* >>>3 Output To STDOUT<<<*3* 3 Output To STDERR 3 Output To STDERR 3 Output To STDERR Wed Nov 23 09:50:17 Westeuropäische Normalzeit 2005 Wed Nov 23 09:50:18 Westeuropäische Normalzeit 2005 Wed Nov 23 09:50:19 Westeuropäische Normalzeit 2005 >>>3 Output To STDOUT<<<*3* 3 Output To STDERR 3 Output To STDERR 3 Output To STDERR >>>3 Output To STDOUT<<<*3* >>>3 Output To STDOUT<<<*3* Wed Nov 23 09:50:20 Westeuropäische Normalzeit 2005 Wed Nov 23 09:50:21 Westeuropäische Normalzeit 2005 Timeoutexit: 1380; trying to kill process Killing '1380' tclsh.exe (1380) pCount: 0 pCount (after loop): 0 ---- [LES]: This looks interesting. Would someone be willing to translate and rewrite the comments and strings in English? '''M.H.''': translated! I hope my english is not too bad to understand the code... If someone would review the code and we are able to make it fool proof, it would be a nice addition to tcllib... ---- How does this compare to BLT's bgexec function? [MHo] I don't know; it must be similar. But I don't need BLT for my bgExec, so my scripts keep small... [US] BLT's bgexec belongs into Tcl's core. For ages. [MHo] But it's not there yet... ---- '''Test whats happening if calling a GUI-App through bgExec:''' # Testing and demonstrating bgExec v1.6, (2) # Test what happens if calling a 32bit-GUI-Tool # 23.11.2005 lappend auto_path [pwd] package require bgexec 1.6 proc dummy {what} { puts >>>$what<<< } set h1 [bgExec notepad.exe dummy pCount] vwait pCount It seems to work! The program is blocking, though. '''Remarks: unfortunally, STDERR-CATCHing with 2>@ doesn't seems to work with Windows 2000.....'''. For 8.4.7 and above, there is or will be a fix, see http://www.tcl.tk/cgi-bin/tct/tip/202.html. ---- MB : Inspired by this package, I extended the bgexec command features and moved it into a SNIT class. The code is publicly available in the [Tclrep] project, in the module jobexec : http://tclrep.cvs.sourceforge.net/viewvc/tclrep/modules/jobexec/ The jobexec class provides the method waitend, which allows to synchronize different jobs which were executed in background (this feature is not available in the current bgexec implementation). The waitend method is based on the vwait Tcl command presented earlier on this page. I also developped the jobscheduler class, which allows to schedule a list of jobs, then to executes them all in background until all are processed. The algorithm is so that at most nbprocs jobs are running at the same time. See jobscheduler.tcl in the [Tclrep] project for more details. ---- [[ [Category Package] | [Category Interprocess Communication] ]]