Reputation: 10573
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.
Edit: I should've mentioned that the CPU usage doesn't spike. What I mean by CPU "hogging" is that no other PHP page seems able to use the CPU whilst the page is sleep()'ing.
Upvotes: 15
Views: 6773
Reputation: 846
It could be that the called page opens a session and then doesn't commit it, in this case see this answer for a solution.
Upvotes: 11
Reputation: 10266
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().
Upvotes: 4
Reputation: 41929
Are you actually seeing the CPU go to 100% or just that no other pages are being served? How many apache-instances are you runnning? Are they all stopping when you run sleep() in of of the threads?
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.
Upvotes: 1