Version 18 of MSW

Updated 2005-02-22 13:12:23

Martin S. Weber

 Ephaeton at gmx dot net

 Homepage still under construction :)

Wiki stuff:

Next to come:


Category Person


This is for suchenwi/ tcl chatroom:

 set ::bexec_runs 0
 set ::bexec_result ""
 set ::bnum 0

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

        # 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 "($bnum)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
 # "many" = enough so that one gets fired and at least TWO are
 # outstanding.
 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.
 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.
 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.
 Lock busy, will retry.
 Lock busy, will retry.
 Lock busy, will retry. 
 # ad infitum... lockfile never goes away.