PHP sleep() silently hogs CPU

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, PHP Tutorial  //  No Comments

I’m running Apache on Linux within VMWare. One of the PHP pages I’m requesting does a sleep(), and I find that if I attempt to request a second page whilst the first page is sleep()’ing, the second page hangs, waiting for the sleep() from the first page to finish.

Has anyone else seen this behaviour? I know that PHP isn’t multi-threaded, but this seems like gross mishandling of the CPU.

What this probably means is that your Apache is only using 1 child process.

Therefore:

The 1 child process is handling a request (in this case sleeping but it could be doing real work, Apache can’t tell the difference), so when a new request comes it, it will have to wait until the first process is done.

The solution would be to increase the number of child processes Apache is allowed to spawn (MaxClients directive if you’re using the prefork MPM), simply remove the sleep() from the PHP script.

Without exactly knowing what’s going on in your script it’s hard to say, but you can probably get rid of the sleep().

Leave a comment

Share This Post

RSS Wordpress News