W
W
websiller2016-08-20 19:33:58
PHP
websiller, 2016-08-20 19:33:58

Why is sleep() behaving strangely?

If you write the following in the test.php file:
sleep(10);
echo "Done";
And then run the script simultaneously in two different browser tabs, then the last tab to run will first wait until the first request is processed (that is, it will wait 10 seconds), and only then will it start counting its 10 seconds. As a result, the time spent waiting for the completion of both requests will be 20 seconds. But why? After all, the first process should not know anything about the second and vice versa. They are running in parallel. Now if you create two different files test.php and test2.php and run them at the same time, then everything will be predictable. Both scripts will complete at the same time, after 10 seconds.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2016-08-20
@websiller

Before sleep, close the session and there will be no such rubbish.
You have file sessions until you close the session, the session file is blocked and the next request under the same session cannot access the file, so it waits until the file is freed, and for this you need to close the session as soon as it is no longer needed.
If you send the second request under a different session, for example from another browser or in incognito mode, then there will be no such nonsense ...

S
Sergey, 2016-08-20
@Pjeroo

It's all about sessions.

P
profesor08, 2016-08-20
@profesor08

Here, the php.exe process starts up for you and until it works, the next candidate in the queue will wait.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question