By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

How does sleep() work

code green
Expert 100+
P: 1,726
Does sleep hand temporary control back to the Operating System
or does it still demand the same resources as when the script is running?
Does it work the same way as VB doEvents().
I am updating data from mssql database to MySql on an overnight scheduled task.
I would like to wait a few secs to ensure the data has gone in,
then run a confidence check on the new data, emailing me of any problems.
Dec 6 '07 #1
Share this Question
Share on Google+
6 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Code Green.

PHP's sleep() function essentially runs through an idle loop for n seconds. It doesn't release any memory, but it should not increase CPU load significantly.
Dec 23 '07 #2

code green
Expert 100+
P: 1,726
Merry Christmas pbmods.
Thanks. That is all I wanted to know.
sleep() will hang the PHP script but still use the same resources.
Nevertheless a query on a database will continue to execute within the DB engine.
Dec 24 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Code Green.

And a merry Chrismakwanzukkah to you as well.

sleep() will hang the PHP script but still use the same resources.
Nevertheless a query on a database will continue to execute within the DB engine.
This is correct, however you'll have to help me understand your situation, because you shouldn't need to force PHP to wait until a DB query finishes executing. PHP will always wait for a DB query to return when you use mysql_query(), et al.
Dec 24 '07 #4

code green
Expert 100+
P: 1,726
A scheduled task runs a script stockAvailabilty.php that produces a report of stock levels from a MySql database.
However, the beginning of stockAvailabilty.php calls another script updateStockLevel.php using exec().
updateStockLevel updates the stock levels in the MySql DB from a MsSql DB.
I was worried that updateStockLevel may still be running because it has been called by exec(),
while the first script compiles its report.
Or even the MySql engine was still updating.
My understanding of exec() is that it can run in the background.
So in my situation I have something like a multi-thread.
Dec 27 '07 #5

pbmods
Expert 5K+
P: 5,821
Heya, Code Green.

I'm sure there is a good reason for this, but why exec() a PHP script rather than include() it?

I suppose what you could do is create a lockfile. Add something like this to the bottom of updateStockLevel.php:

Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. .
  4. unlink('/path/to/lockfile');
  5.  
And in stockAvailability.php:
Expand|Select|Wrap|Line Numbers
  1. touch('/path/to/lockfile');
  2. exec('php /path/to/updateStockLevel.php');
  3.  
  4. while( file_exists('/path/to/lockfile') )
  5. {
  6.     sleep(1);
  7. }
  8.  
Dec 30 '07 #6

code green
Expert 100+
P: 1,726
Thanks, pbmods I will study that technique.
I'm sure there is a good reason for this, but why exec() a PHP script rather than include() it?
Well updateStockLevel is a standalone program that includes functions and classes that are common to stockAvailability.
So I had function/ class redeclaration problems.
Jan 3 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.