Ask Ben: Limiting The Amount Of Time A Block Of Code Can Run (Attempt II)

Posted April 2, 2007 at 10:00 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

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.

  • <!--- Try to execute the following block of code. --->
  • <cftry>
  •  
  • <!---
  • By setting a request time out for this page, we are
  • forcing it to have a maximum amount of time that it
  • can run. Since the black-boxed code comes after it,
  • it will be dominated by this CFSetting.
  • --->
  • <cfsetting
  • requesttimeout="5"
  • />
  •  
  •  
  • <!---
  • This is our black-boxed piece of code. Not
  • sure how it works, but we know that 2% of the
  • time it runs way longer than it should and
  • crashes the page.
  • --->
  • <p>
  • This will only be allowed to execute for
  • a maximum of 5 seconds.
  • </p>
  •  
  •  
  • <!---
  • Catch an errors that get thrown from our
  • code exceeding the CFSetting timeout attribute.
  • --->
  • <cfcatch>
  •  
  • <!---
  • Our black-boxed piece of code has run too
  • long. Use this opprotunity to set default
  • values so that you might be able to recover
  • from this timeout.
  • --->
  •  
  • <!---
  • Since the rest of the page will take some time
  • to execute, set a new time out for this request.
  • --->
  • <cfsetting
  • requesttimeout="60"
  • />
  •  
  • <!--- Perform recovery code here. --->
  •  
  • </cfcatch>
  •  
  • </cftry>

Hopefully this is better.




Reader Comments

Apr 3, 2007 at 8:54 AM // reply »
56 Comments

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.


Apr 3, 2007 at 9:02 AM // reply »
11,238 Comments

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.


Apr 5, 2007 at 12:26 AM // reply »
172 Comments

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.


Feb 23, 2009 at 11:54 AM // reply »
3 Comments

Just came across this issue ... as there is no timeout property for CFFEED, it's not possible to abort a request e.g. after 5 seconds ... http://cfstuff.blogspot.com/2009/02/cffeed-not-usable-due-to-missing.html


Jan 17, 2012 at 11:30 AM // reply »
1 Comments

YES YES YES!!! thank you so much for this post!

I was having trouble with a cffeed from an external site which was timing out constantly so getting a horrible error message on my page,

but i stumbled across this solution after lots of googling and now i have it so if my first feed times out then it reads in an alternative feed so no more errors!

I know your post is quite old but its just helped me out so much!


Jan 11, 2013 at 3:24 PM // reply »
2 Comments

Works great Ben. Simple and sweet. Was something I had been trying to accomplish for a few, sure to add to it eventually.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools