views:

2571

answers:

3

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.

+1  A: 

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.

Espo
+2  A: 

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().

rix0rrr
You've tamed the notion a little with the word 'probably', but I still don't see how you can justify expecting to be able to remove bits of code you've never seen without breaking things?
JoeBloggs
I'm sorry I didn't word my remark more clearly.I didn't mean you could just get rid of the sleep and the application would keep on working. I meant that in all likelihood the sleep() is not there for a good reason, and the application could probably be rewritten to work without the sleep().
rix0rrr
+2  A: 

Could be that the called pages open a session and them don't commit it, in this case see this answer for a solution.

EndelWar
If you have php.ini configured with session.auto_start set to 1, it will open the session before it runs any code, and keep it open until you explicitly close it, or the page reaches the end. The second request will seemingly hang even before it runs the first line of code.
Joeri Sebrechts