[Looking for Charlie's main web site?]

CF 8 Hidden Gem: new Sleep() function to pause a current request

Note: This blog post is from 2007. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as necessary.
Ever wanted to pause a currently running request? You can now, in CF8, using the new sleep() function. CF8's chock full of hidden gems. Indeed, I make note of nearly 50 of them in a user group talk I've started doing. One of them is this new Sleep() function.

Some will recognize it as an easier way to call the java thread.sleep method (as many have noted, and I wrote about back in 2002). It's been added primarily as part of the multi-threaded processing (CFTHREAD) feature, such as when one thread needs to wait upon another.

But it can be useful sometimes on its own, such as when you want to simulate a long-running request for any reason. (And it's a whole lot more server-friendly than doing a huge cfloop, since a sleep call doesn't really spin the CPU. It literally halts the current request, putting it to "sleep".)

As with the sleep method in java, sleep() takes a number in milliseconds, so for 5 seconds, use 5000. You can use it in CFSCRIPT, or in a simple CFSET:

<cfset sleep(5000)>

Why might you want to simulate a long-running request? There are many reasons. Perhaps to test some logic in how CFLOCK failures work, or to cause a page to appear in the new CF8 server monitor--or one of the long-existing monitor tools (to make sure you're seeing what you think you should be seeing).

Still another reason to use it has to do with another hidden gem, this one in CF7. I'll write about that shortly.

For more content like this from Charlie Arehart: Need more help with problems?
  • If you may prefer direct help, rather than digging around here/elsewhere or via comments, he can help via his online consulting services
  • See that page for more on how he can help a) over the web, safely and securely, b) usually very quickly, c) teaching you along the way, and d) with satisfaction guaranteed
Comments
Wow, great tip charlie! I didn't even know they had added that but it could be VERY useful for dealing with Ajax race conditions. That's an issue that I still haven't solved.. let me give an example from my new Project Tracker:

Let's say I make a user an admin of a project - I then redraw the div listing the project admins. Sometimes the database hasn't caught up yet, though.. I need to give it a few milliseconds to get the update in there before requerying the database. This would be a great solution since I could deal with a quarter second delay redrawing that part and that should be plenty of time to let the update go through.

Thanks! The CF8 goodness just keeps coming :)
<cffunction name="sleep" output="No" returntype="void" hint=
   "Pauses execution for a number of milliseconds."
>
   <cfargument name="milliseconds" required="Yes" type="numeric"/>
   <cfset var thread = CreateObject("java", "java.lang.Thread")/>
   <cfset thread.sleep(ARGUMENTS.milliseconds)/>
</cffunction>
Darin, did you read all of the blog entry? :-) I said in the opening of the 2nd paragraph that "Some will recognize it as an easier way to call the java thread.sleep method (as many have noted, and I wrote about back in 2002). " In the link I offered, I had pretty much the same code.

I do understand that it's not really "new" functionality. It's just simpler (just like the CFLOOP ARRAY--nothing we couldn't do before, just easier).

Anyway, either of our code examples can be helpful to those still running CF7 (or 6, or indeed back to 4.51, when Java integration was first enabled--prior to 6 you had to point CF to the JVM.)

Indeed, Joe, if you're challenges are happening on 7, then do know that you don't NEED to wait for CF8 to solve that problem. Cheers, gents.
Charlie,

I didn't see your post in 2002, I accredited the find to Peter Freitag when I wrote a function for cflib.org back in 2003:

http://www.cflib.org...

Glad to see this as a native function. Thanks for blogging it!
Oh now worries, Nathan. It's so long ago, I can't remember who I'd learned it from. It could have been Pete--he's posted some amazing stuff over the years. :-)

Anyway, we all just keep passing the baton, as we proceed in the race. :-) Thanks, though.
BlueDragon has had <CFPAUSE interval=""> for years now.

But you knew that already Charlie didn't you?
# Posted By Alan | 6/15/07 2:49 PM
Yep, no slight intended there, Alan. Just wasn't thinking of it as I wrote.

After 15 months (since I left New Atlanta), I guess my memory may be fading for remembering to make such connections whenever I can. Clearly I used to, all the time, and I still do it often (sometimes still more often than many would prefer).

But clearly, it's no longer at the forefront of my attention, and occasionally it may seem to some that I've even turned my back on BD. I have not. I even have clients who use it.

Still, as far as making references to it, it's just one of those things where I'm "damned if I do, and damned if I don't". It ain't easy bein' me. :-)

PS I know you weren't "damning" me, Alan. I'm referring to a much broader context.
Charlie,

Thanks for sharing about the server monitoring; It is one of my favorite features.


I added your CF8 feed to the Scorpio Yahoo Pipe:
http://pipes.yahoo.c...


More here:
http://www.nodans.co...

Dan Wilson
Yep, that's a really cool idea, Dan. I'd seen it before and was looking forward to seeing how it evolves. Thanks for doing it.
Copyright ©2024 Charlie Arehart
Carehart Logo
BlogCFC was created by Raymond Camden. This blog is running version 5.005.
(Want to validate the html in this page?)

Managed Hosting Services provided by
Managed Dedicated Hosting