Is it possible to tie up the server with a while loop?
Moderator: General Moderators
-
ryeterrell
- Forum Newbie
- Posts: 1
- Joined: Wed Sep 27, 2006 9:21 am
Is it possible to tie up the server with a while loop?
Here's the deal. I have two users logged on, users A and B, and I have user A waiting for user B to do something. I keep user A waiting with a while loop in the php code.
Thing is, it seems like user B can't do anything while that while loop is running.
So my question is, if PHP code is running on the server, does it keep other code from running?
Thanks
Rye
Thing is, it seems like user B can't do anything while that while loop is running.
So my question is, if PHP code is running on the server, does it keep other code from running?
Thanks
Rye
No, each request is processed in parallel. A locking may occur if both users compete for a server resource, like unshared access to a file, but this is rare, probably your problem comes from somewhere else.
Anyway, your approach doesn't "smell" right, for starters the server will kill a stale script after a timeout (which is a GOOD thing, as it doesn't allow remote users to tie up server resources, so don't go thinking of going around it with set_time_limit(0)!)
A better way is to have a script that checks if B is done. If not, the script prints a meta-refresh tag thus instructing A's browser to try again in a while. In that way the waiting loop is transferred at the client.
Anyway, your approach doesn't "smell" right, for starters the server will kill a stale script after a timeout (which is a GOOD thing, as it doesn't allow remote users to tie up server resources, so don't go thinking of going around it with set_time_limit(0)!)
A better way is to have a script that checks if B is done. If not, the script prints a meta-refresh tag thus instructing A's browser to try again in a while. In that way the waiting loop is transferred at the client.
Using AJAX you can poll the server every n seconds using a script that generates a lightweight response. Rather than having a while() loop waiting for User B to do something, you have a script User A calls via AJAX that returns a yes or a no, and he can sit and watch a pretty "waiting" screen at the same time. Perhaps even with a timer or a progress bar.bokehman wrote:Are you serious? Script A is stuck in a loop using up server resources. This makes script B extremely sluggish. How is AJAX going to help?onion2k wrote:This is the sort of situation AJAX is really handy for.
There's no while() loop, the server isn't tied up at all, and User A isn't generating a complete page response as he would if it were done with a meta refresh.
As I understand it ryeterrell is putting user A in a while() loop and checking a condition that an action by user B will meet. For example, the while loop could check a database value that the script user B calls updates.bokehman wrote:Maybe I misread the question. My understanding was the server was stuck in a loop by accident.onion2k wrote:Rather than having a while() loop
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Actually it is very, very EASY for a single PHP program to completely tie up a server unless you have some kind of process load limiting installed.
The biggest culprit is the use of a WHILE function that has no end. The same thing can happen with a FOR function is the values being checked never change or keep getting reset. You will quickly max out the server load. I know I have done it on my development server a number of times in the past. I usually have to kill the process through command line when that happens. Any other connections to the server WILL slow to a virtual crawl until the process has finished or been killed.
You should NEVER use a loop of any kind to create a wait state in PHP because it will cause a load spike for the CPU.
The biggest culprit is the use of a WHILE function that has no end. The same thing can happen with a FOR function is the values being checked never change or keep getting reset. You will quickly max out the server load. I know I have done it on my development server a number of times in the past. I usually have to kill the process through command line when that happens. Any other connections to the server WILL slow to a virtual crawl until the process has finished or been killed.
You should NEVER use a loop of any kind to create a wait state in PHP because it will cause a load spike for the CPU.
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
Its like testing a C++ program that was poorly made:
Great way to crash a system.
Code: Select all
int main() {
int a;
int b;
a == 0;
b == 1;
while(a < b) {
cout << "god damn it\n";
}
}- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
'=', not '=='waradmin wrote:Its like testing a C++ program that was poorly made:
Great way to crash a system.Code: Select all
int main() { int a; int b; a == 0; b == 1; while(a < b) { cout << "god damn it\n"; } }
and yes. i've done this and it's hard to get out of. especially when the programs you were making were fullscreen usually lol
I want to test the limits of my system by running a simple counting script. It would be interesting to know how much the computer will take before crashing. (Ive crashed my Mac OS x86 system doing this after it reaches about 8,000,000,000MrPotatoes wrote:and yes. i've done this and it's hard to get out of. especially when the programs you were making were fullscreen usually lol
Say using:
Code: Select all
#include <iostream>
using namespace std;
int main() {
int a;
double b;
cout << "Input ending number:\n";
cin >> b;
a = 0;
while(a < b)
{
cout << a;
a++;
}
return 0;
}