Problem with function session_start() (works slowly)

隐身守侯 提交于 2019-12-06 04:29:14

问题


I have a problem wtih session_start() on primary server. When I load page for the first time, it takes less than 1 second to complete request. If I'll wait for approximately 12-15 seconds and then reload page, time of loading will be the same. But when I'm trying to refresh page after, for example, 3 or 5 seconds after initial loading, time of the server's response equals 10 seconds.

I made some tests to define a bottleneck in my script and I found out, that function session_start() executes for 9.8 seconds. I'm using PEAR package HTTP_Session2. Here is code snippet:

HTTP_Session2::useCookies(SESSION_USE_COOKIE);
/* Next line was added to make logging of execution time possible. */
self::writeToFile('HTTP_useCookies(1) -> '.self::getWorkTime()); 
HTTP_Session2::start("SID");
self::writeToFile('HTTP_start(2) -> '.self::getWorkTime());
HTTP_Session2::setExpire(time() + SESSION_EXPIRE);
self::writeToFile('HTTP_setExpire(3) -> '.self::getWorkTime());

Text of the logs:

//First loading (13:34:35)
HTTP_useCookies(1) -> 0.00038
HTTP_start(2) -> 0.00077
HTTP_setExpire(3) -> 0.00090

// Second loading (13:34:39)(4 seconds later)
HTTP_useCookies(1) -> 0.00029
HTTP_start(2) -> <<<<<< 10.80752 >>>>>
HTTP_setExpire(3) -> <<<<<< 10.80780 >>>>>

//Third loading (13:34:56)
HTTP_useCookies(1) -> 0.00041
HTTP_start(2) -> 0.00071
HTTP_setExpire(3) -> 0.00083

So I found, that problem is in the HTTP_Session2::start() function. Code of the function HTTP_Session2::start():

public function start($name = 'SessionID', $id = null)
{
  self::name($name);
  if (is_null(self::detectID())) {
    if ($id) {
      self::id($id);
    } else {
      self::id(uniqid(dechex(rand())));
    }
  }
  session_start();
  if (!isset($_SESSION['__HTTP_Session2_Info'])) {
    $_SESSION['__HTTP_Session2_Info'] = self::STARTED;
  } else {
    $_SESSION['__HTTP_Session2_Info'] = self::CONTINUED;
  }
}

I can't understand what is the cause of time delay. Probably, there are wrong Apache settings on the server. Or, maybe, there are some 'locks' on files with session information.

Maybe, someone has already encountered such problems and can help to solve it.


回答1:


The file containing the session's informations is locked during the time PHP serves a request.

So, if you have one PHP script (using a session) that's currently being executed, and the same user sends another request, the second request will wait until the first is finished.



来源:https://stackoverflow.com/questions/1914242/problem-with-function-session-start-works-slowly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!