MBajaria
MBajaria

Reputation: 19

TCL How to end infinite loop

I have tcl(myscript.tcl) script which have while {1} loop which is called(executed) via another tcl script(test.tcl). and if myscript.tcl stuck because of while loop then how can terminate using test.tcl after particular time period( e.g 10 seconds)

Upvotes: 1

Views: 679

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137557

If you run the script myscript.tcl within a subordinate interpreter, you can set a time limit on the execution via interp limit so that the child interpreter can't go on forever; when the limit is reached, an error is generated which bubbles out to the outer managing interpreter. We explicitly test whether the code that supports limits can break out of infinite loops.

set helper [interp create]
# You might need to do some more setup here; do that before setting up the limit
interp limit $helper time -seconds [expr {[clock seconds] + 10}]
catch {interp eval $helper [list source myscript.tcl]}

Upvotes: 2

Related Questions