0

I have a piece of code that has a procedure, I only want the proc to trigger, at most every 2 minutes. If it has triggered within the last 2 min, then it should just exit. Currently it has a small delay before it executes (after 5000), but what this seems to do as well, is queue any other execution requests that have occurred in the quiesce time (5 seconds) and then just pummel the queued commands out in a flurry of activity.

Obviously this is missing a significant portion, but
I have considered doing something with variables like:

#gets current time since epoch as var0
set timer0 clock format [clock seconds] -format %s
#gets current time since epoch as var1
set timer1 clock format [clock seconds] -format %s
#calculates elapsed time since epoch in seconds, converts seconds to minutes
set split [ expr (($timer0 - $timer1)/60)/60 ]
if { $split > 2 } {
    my_proc $maybe_1var $maybe_2vars $maybe_3vars  
} else {  
    exit super gracefully  
}  

I can provide snippets of my current code if you like. It is just not nearly as elegant as I imagine it could be, and I am not sure if there are better ways of doing this in Tcl.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Matty
  • 111
  • 8

1 Answers1

1

One possible solution:

set lastExecuted ""

proc foo {} {
    global lastExecuted
    set currentTimestamp [clock scan seconds]
    if {$lastExecuted != "" && ($currentTimestamp - $lastExecuted) > 120} {
        exit
    }
    set lastExecuted $currentTimestamp
    # continue execution
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Sharad
  • 9,282
  • 3
  • 19
  • 36
  • this is far more elegant than my bastardised code. Just as a matter of interest, the issue I have had with this sort of thing in the past, is with the lastExecuted variable, even though it is set at the end of the procedure, it would get flushed each time the script is called - would that not be the case here? So, in effect except for a brief while between executions, lastExecuted would have a null value. Or was that just my poor variable declarations, within Tcl possibly? – Matty Apr 18 '16 at 05:49
  • You are right - if the script is triggered each time, the variable would be flushed if it is kept within the script. It would working in case of a running application (say a daemon). For the former case, you can externalize this variable in say a config file (or a database if that's feasible for you). The script would then check the presence of this config file, read the variable from it and will be able to persist information across multiple executions. – Sharad Apr 18 '16 at 06:12
  • Actually, I have bigger logic issues than that at the moment.. lastExecuted is primed with a null value, and it is not then updated with an actual date/time until the procedure foo, is called, and then, the first if statement must be true (and it is not, because at this stage $lastExecuted == "" still), before the code will execute to where I think possibly an additional else statement should be to prime the lastExecuted variable with the required date/time stamp.. and execute my code.. – Matty Apr 18 '16 at 06:19
  • Most of my Perl and Python scripts of this type use MySQL for data storage and retrieval, so this should not be an issue...though this is something else I have not done in Tcl, and I am hoping it is not tragically painful, else I'll just burn the whole thing to the ground, and migrate the code over to Perl or something.. – Matty Apr 18 '16 at 06:32
  • @Donal: Thanks for the edit. But the subtract operation won't work without expr: % 4 - 2 invalid command name "4" % expr 4 - 2 2 % – Sharad Apr 18 '16 at 08:58