Bob Vesterman
Bob Vesterman

Reputation: 1211

PHP: Can only a single instance of a particular script be executed at once?

I have a simple little script that prints the current time, sleeps for ten seconds, and prints the current time again:

<?php
echo "<html><head><title>title</title></head><body>";
echo "<p>Pre-sleep: " . date("H:i:s") . "</p>";
sleep(10);
echo "<p>Post-sleep: " . date("H:i:s") . "</p>";
echo "</body></html>";
?>

I actually have that exact same code in two different pages (say "blah1.php" and "blah2.php"). If I use two different browsers to hit those two different pages (essentially) simultaneously, the two pages seem to run (essentially) simultaneously:

blah1.php output:

Pre-sleep: 11:15:26

Post-sleep: 11:15:36

blah2.php output:

Pre-sleep: 11:15:27

Post-sleep: 11:15:37

But if I instead use the two different browsers to both hit the same copy of the script, it is as if the second instance doesn't even start executing until the first finishes:

blah1.php instance 1 output

Pre-sleep: 11:09:07

Post-sleep: 11:09:17

blah1.php instance 2 output

Pre-sleep: 11:09:17

Post-sleep: 11:09:27

So it seems like PHP -- or at least my particular PHP configured in the way it's currently configured -- will queue up requests for any particular script, despite being able to simultaneously serve requests for separate scripts? Am I misunderstanding?

Is this expected? And is it a general thing? Or is it perhaps related specifically to the sleep() function, or to the particular server that's actually running the PHP script (Apache, in my case), or the OS, or something like that?

Can I rely on this behavior (that requests for a particular page are queued up and never executed simultaneously)? On the other side, can I prevent this behavior?

Thanks in advance.

UPDATE: I now believe the issue to be unrelated to PHP, Apache, or in fact anything on the server side.

I suddenly realized that when I said "two different browsers", that's actually inaccurate: I had actually made the requests from two different windows of Firefox (on the same computer). So, I instead tried with one Firefox and one IE, and both ran simultaneously on the server side.

I then tried with two different windows of Firefox again, and watched a web sniffer. The second window did not even send the GET request until the response for the first window had completed.

So it seems like this behavior is on Firefox: At least under certain conditions, if you try to make two requests from Firefox to the same URL, Firefox will queue them up instead of processing them at the same time.

Upvotes: 4

Views: 1109

Answers (2)

Uriel
Uriel

Reputation: 464

If you need to access the same PHP instance so you need to use threads: example

... but may be that's too complex for just being testing MySQL's GET_LOCK.

On the other hand, may be your result are because the first call are blocking the lock name.

GET_LOCK(str,timeout)

Tries to obtain a lock with a name given by the string str, using a timeout of timeout seconds. A negative timeout value means infinite timeout. The lock is exclusive. While held by one session, other sessions cannot obtain a lock of the same name.

Returns 1 if the lock was obtained successfully, 0 if the attempt timed out (for example, because another client has previously locked the name), or NULL if an error occurred (such as running out of memory or the thread was killed with mysqladmin kill).

Upvotes: 0

Tom Wright
Tom Wright

Reputation: 2861

You can execute as many instances of a PHP script that you like. When you visit your script through the browser you are accessing it through a web server. When you access the script through the Web server all that happens is that the server executes 1 instance of the script.

From the above, it looks as though you have your web server set up to queue up incoming requests which has the above side affect.

Upvotes: 1

Related Questions