V
V
vdovin752013-11-22 11:23:44
PHP
vdovin75, 2013-11-22 11:23:44

Why does the session_start() function in PHP take a long time on the third and subsequent simultaneous request?

There is a simple code:
<?php
$microtime = microtime(true);
session_start();
print ( microtime(true) - $microtime );
sleep(10);
?>
I open 5 tabs in the browser and run this script 5 times for simultaneous execution. The first two tabs running show that the session_start() function completes in 1 millisecond. Third tab - 10 seconds! Fourth - 20 seconds! Fifth - 30 seconds! It turns out that starting from the third copy, the function waits until the work of the previous ones is completed?
I change the code a little so that every time there is a new session:
<?php
$microtime = microtime(true);
session_name( "my_" . rand(100000, 999999) );
session_start();
print ( microtime(true) - $microtime );
sleep(10);
?>
The delay disappears, i.e. all five running copies are executed in parallel, they do not wait for each other, and the session_start () function in them is executed equally quickly - in about 1 millisecond.
What could be the reason for this delay? Maybe there are some settings for PHP that limit the number of simultaneous processes for one session? Checked on PHP - 5.5.3 and 5.2.1.
I searched the Internet, found similar questions even for PHP 4, but I did not find an answer to the question.
Thanks to everyone who will suggest an idea in which direction to look.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
H
Hint, 2013-11-22
@vdovin75

Requests with the same session cannot be executed in parallel. The session data file is locked for the duration of the script execution (from the moment session_start to the moment the session is closed, for example, by the session_write_close function). How else can you protect against simultaneous modification of session data by several copies of the script? And so there is a guarantee that these sessions are not in an intermediate state.

M
Masterme, 2013-11-22
@Masterme

Dig aside "where does PHP store sessions by default?"

V
vdovin75, 2013-11-22
@vdovin75

Yes, I checked. A temporary file is created in the corresponding folder. And besides, the first two copies of the script work fine. The delay goes only from the third. If there were any blocking of the file, then the second one would also work with a delay. So I argue.

V
vdovin75, 2013-11-22
@vdovin75

Thanks a lot for the answers! I also thought in this direction, it was only embarrassing that the second copy of the script worked out quickly, so the blocking option was eliminated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question