Version 13 of MSW

Updated 2005-02-22 12:24:22

Martin S. Weber

 Ephaeton at gmx dot net

 Homepage still under construction :)

Wiki stuff:

Next to come:


Category Person


This is for suchenwi:

 set ::bexec_runs 0
 set ::bexec_result ""

 proc bexec what {
        global bexec_runs bexec_result
        while {[catch {set fp [open [file join $::env(HOME) tmp cmd_run_lock_$::env(USER)_[pid]] {WRONLY CREAT EXCL}]}]}  {
                        puts stderr "Lock busy, will retry."
                        after 200 "set ::retry 0"
                        vwait ::retry
        }
        puts stderr "Got the Lock for \"[string range $what 0 99]...\"."

        if {$bexec_runs} then {
                puts stderr "waiting for other bexec(s) to be done."
                vwait bexec_runs
        }
        # reset results
        set ::bexec_result ""
        puts stderr "Executing $what..."
        set bexec_runs 1
        set f [open |$what]
        #fconfigure $f -translation none
        fileevent $f readable [list bexec_read $f]
        vwait bexec_runs
        file delete -force [file join $::env(HOME) tmp cmd_run_lock_$::env(USER)_[pid]]
        puts stderr "Released the Lock for \"[string range $what 0 99]...\"."

        return $bexec_result
 }

 proc bexec_read {f} {
        set data [read $f]
        if [eof $f] {
                close $f
                set ::bexec_runs 0
        }
        append ::bexec_result $data

}

 # demo1:
 set tfile [open tst.sh w]
 puts $tfile {#!/bin/sh
 echo "output1"
 sleep 3
 echo "output2"}
 close $tfile

 puts [bexec "sh tst.sh"]
 puts [bexec "sh tst.sh"] ;# <-- this one will wait.

 # demo2:
 pack [button .b -text "run" -command {puts [bexec "sh tst.sh"]}] -expand 1 -fill both

Output:

 Got the Lock for "sh tst.sh...".
 Executing sh tst.sh...
 Released the Lock for "sh tst.sh...".
 output1
 output2

 Got the Lock for "sh tst.sh...".
 Executing sh tst.sh...
 Released the Lock for "sh tst.sh...".
 output1
 output2

 # hit the run button one time
 Got the Lock for "sh tst.sh...".
 Executing sh tst.sh...
 Released the Lock for "sh tst.sh...".
 output1
 output2

 # hit the run button many times
 Got the Lock for "sh tst.sh...".
 Executing sh tst.sh...
 Released the Lock for "sh tst.sh...".
 output1
 output2

 Got the Lock for "sh tst.sh...".
 Executing sh tst.sh...
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 # 1 sec over
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 # 2 sec over
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 # here the lock should go away.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry. 
 # ad infitum...