Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Shell sleep command avoids CPU utilization glitch System
I needed to put a short pause into an AppleScript I was writing. I first used a delay 10 statement to wait 10 seconds. I noticed, however, that processor utilization went up to near 100% for the 10 seconds. I switched to a do shell script "sleep 10" statement, and processor utilization remained virtually unchanged during the sleep period.

Based on this observation, I'll be using the latter statement above, when needed, to make my AppleScripts more utilization friendly and cooperative.
    •    
  • Currently 5.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[13,276 views]  

Shell sleep command avoids CPU utilization glitch | 6 comments | Create New Account
Click here to return to the 'Shell sleep command avoids CPU utilization glitch' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Actually a bug
Authored by: skab on Nov 25, '02 02:17:49PM

That's a good hint for AppleScript programmers, but actually I consider this a bug that you should report to Apple. A built-in command that simply waits 10s shouldn't use any, let alone that much CPU.



[ Reply to This | # ]
Actually a bug
Authored by: mervTormel on Nov 25, '02 07:09:13PM

ditto



[ Reply to This | # ]
Actually a bug
Authored by: Ripcord on Aug 28, '05 07:23:06PM

Believe it or not, this is still a bug in 10.3.9 and 10.4.2.



[ Reply to This | # ]
Curious
Authored by: greggo on Nov 25, '02 07:30:11PM

I was curious about this, so I ran a simple AppleScript...

beep
delay 10
beep

While running 'top -u' in a terminal window. The %CPU rate for the ScriptEditor remained above 95 & 100+ %CPU usage during the 10 second delay. Wow!

Then I ran...

beep
do shell script "sleep 10"
beep

With %0.0 CPU usage for the ScriptEditor during the 10 second delay.

Great Tip, thanks.

greggo



[ Reply to This | # ]
Definitely a bug
Authored by: teferi on Dec 02, '02 02:45:50AM

If it eats 100% cpu, the 'delay' command in AppleScript is busy-waiting instead of , well, using the UNIX sleep() function which is designed for cases like this.
Apple ought to fix this; it's probably a simple patch.



[ Reply to This | # ]
Simple Fix
Authored by: Morgoth on Dec 02, '02 10:10:00PM

They could make 'delay' an alias for 'sleep'.

The backend function for 'delay' would simply pass on it's arguments to 'sleep'.



[ Reply to This | # ]