Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set a max timeout for the script execution?

Status
Not open for further replies.

BTCMan

Programmer
Jun 17, 2005
21
BR
Hi people,

I wrote a TCL script and now I am facing problems because I did not specify a timeout condition - I don't know how to do that in TCL!Basically I want that my program continues its execution but ensuring that the timeout limit is never crossed. This is what I tried (among other stuff) but did not work:
-----------------------------------
proc setTimeoutProtection {} {
puts "Inside setTimeoutProtection proc"
global ze

# setting timeout to 1 second
set ze [after 1000 {
puts "timeout condition reach! Leaving program"
puts "end of tcl program due timeout!"
exit 1
}]

puts "going out of setTimeoutProtection proc"
}
#-----------------------
puts "start of tcl program"
setTimeoutProtection
puts "but can do other things"

# setting loop to 5 seconds - which is bigger than timeout value (1 sec)
# this is supposed to trigger the timeout and exit the program
# with return code "1"
for {set i 0} {$i<5} {incr i} {
puts "inside loop that consumes time to trigger timeout condition."
exec sleep 1
}

puts "end of tcl program WITHOUT timeout!"
exit 0
-----------------------------------
Execution output:
start of tcl program
Inside setTimeoutProtection procedure
going out of setTimeoutProtection procedure
but can do other things
inside loop that consumes time to trigger timeout condition.
inside loop that consumes time to trigger timeout condition.
inside loop that consumes time to trigger timeout condition.
inside loop that consumes time to trigger timeout condition.
inside loop that consumes time to trigger timeout condition.
end of tcl program without timeout!
-----------------------------------

Does anybody knows how to set a timeout in a single thread pure tcl application (without expect).Thanks

BTCMan
 
Something like this?
Code:
set n 0
set txtlist {foo bar bar foofoo barbar foofoofoo barbarbar}

proc doCount {cn} {
global n
set i 0
                 while {$i < $cn} {incr i ; puts $i}
		 if {$n >= 5} {exit}
}


proc junk {txt} {
global txtlist n
   puts $txt 
   if {$n > 12} {puts "The end"; exit}
   flush stdout 
   incr n
   after [expr int(1 + rand() * 20000)] [list eval junk [lindex $txtlist [expr int(1 + rand() * [expr [llength $txtlist] - 1])]]]
   set tmer [expr int(1 + rand() * 45)]
   puts "Timer at $tmer"
   doCount $tmer
 }

junk "The beginning...."
junk "More beginnings..."
vwait forever
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top