TABLE OF CONTENTS


::pwtk::run_wait

SYNOPSIS

proc ::pwtk::run_wait {args} {

USAGE

   run_wait ?id1? ?id2? ...

PURPOSE

Wait for the specified 'run -bg' background processes to finish.

If ::pwtk::run_wait is called without arguments, the command waits for all the 'run -bg' background processes to finish.

ARGUMENTS

SOURCE

    variable run

    if { ! [info exists run(bg.ID)] } {
        # nothing to wait for; there are no 'run -bg' background processes
        return
    }

    set all_procs 0
    set remedy_activated 0
    
    # default == wait for all processes
    if { $args eq {} } {
        set args [::pwtk::run_ids]
        set all_procs 1; # wait for all bg procs to finish
    }

    set args [concat {*}$args]
    foreach id [::pwtk::lunique $args] {
        ifexist run(bg.PID,$id) {
            lappend pids $run(bg.PID,$id)
            lappend ids  $id
        }
    }

    ifexist pids {
        set pr [expr { [llength $pids] > 1 ? "es" : "" }]
        print "Waiting for the background process$pr $pids to finish ...\n"
        
        foreach pid $pids id $ids {
            # wait for the background process $pid to finish
            pid_wait $pid
            unset run(bg.PID,$id)
            
            # now execute a remedy & postrun scripts for $pid
            ::pwtk::input::pushpop {
                variable state
                ::pwtk::input::load_state $run(bg.peek,$id)
        
                if { [remedy $run(bg.pname,$id)] } {
                    set remedy_activated 1
                    if { [checkForError $run(bg.output,$id) $run(bg.prog,$id) 0] } {
                        set remedy [remedy $run(bg.pname,$id) get]
                        ::pwtk::infoMsg "An error occured in '$run(bg.output,$id)'.\nTrying to remedy it by using the remedy script:\n$remedy"
                        ::pwtk::input::pushpop {
                            remedy $run(bg.pname,$id) clear
                            eval $remedy
                            eval $run(bg.run_cmd,$id)
                        }
                    }
                } else {
                    checkForError $run(bg.output,$id) $run(bg.prog,$id) $state(stopOnError)
                }
                
                # handle "postrun"
                
                set pname $run(bg.pname,$id)
                if { $pname eq "hp.x" } {
                    # make a backup copy of $prefix.Hubbard_parameters.dat
                    runHP_backup_HP_ $run(bg.output,$id)
                }
                set postruns [::pwtk::postruns_ $pname]
                if { $postruns ne {} } {
                    foreach name $postruns {
                        # should a postrun be done inside pushpop? NO!
                        #::pwtk::print "Evaluating the postrun script:\n$state($name)"
                        eval $state($name)        
                    }
                }            
            }
        }
    }

    if { $all_procs && $remedy_activated } {
        # there may be 'remedy' unfinished bakcground processes
        ::pwtk::run_wait
    }
}