I have this application that uses a black-boxed piece of functionality. 98% of the time, this black-boxed functionality runs very fast as expected. Sometimes though, for reasons I cannot yet debug, this function takes so long that the page times out. Is there a way to make sure that just this block of code only executes for X number of seconds?
Ok, my last attempt at answering this question was a total failure that demonstrated my lack of understanding regarding the CFLock tag. Thankfully, Christoph Schmitz has shown my the light. I have re-attempted an answer using the CFSetting / RequestTimeOut tag and attribute. I am not 100% sure it can be done this way, but testing seems to say that Yes, it can.
Launch code in new window » Download code as text file »
Hopefully this is better.
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Print Page
Throwing And Catching A File Using CFHttp For Both Actions
Ask Ben: Limiting The Amount Of Time A Block Of Code Can Run
while this solution would work for a calling template, it won't work within a function. I think what the person is asking is if they can make a CFC method time out after a certain amount of time. Now personally I have no idea how to do this.
Posted by tony petruzzi on Apr 3, 2007 at 8:54 AM
Yeah, this seems to be a very difficult task (or something simple that I am just not thinking of). I saw a presentation on Blue Dragon not so long ago and they had a CFThread tag... I wonder if the CFThread tag has a timeout attribute; that seems like something that would do the trick here - wrapping the "locked down" code in a CFThread tag and forcing a small timeout.
Posted by Ben Nadel on Apr 3, 2007 at 9:02 AM
When I use "while" loops, I often do something like this:
-----------------------
<!--- Set the time before entering the loop --->
<cfset var timeout = now() />
<cfloop condition="TRUE">
...do something here...
<!--- End the loop if it has run for more than five seconds --->
<cfif dateDiff("s", timeout, now()) GTE 5>
<cfbreak />
</cfif>
</cfloop>
-----------------------
Of course, that won't prevent the processing during an individual iteration of the loop from running more than five seconds.
Posted by Steve on Apr 5, 2007 at 12:26 AM