apache 函数 session_start() 的问题(工作缓慢)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1914242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with function session_start() (works slowly)
提问by Alex
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.
我session_start()在主服务器上有问题。当我第一次加载页面时,完成请求不到 1 秒。如果我等待大约 12-15 秒然后重新加载页面,加载时间将相同。但是,当我尝试在初始加载 3 或 5 秒后刷新页面时,服务器的响应时间等于 10 秒。
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:
我做了一些测试来定义脚本中的瓶颈,我发现该函数session_start()执行了 9.8 秒。我正在使用 PEAR 包 HTTP_Session2。这是代码片段:
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():
所以我发现,问题出在HTTP_Session2::start()函数上。函数代码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.
我无法理解时间延迟的原因是什么。可能是服务器上的 Apache 设置错误。或者,可能对具有会话信息的文件有一些“锁定”。
Maybe, someone has already encountered such problems and can help to solve it.
也许,有人已经遇到过这样的问题,可以帮助解决它。
回答by Pascal MARTIN
The file containing the session's informations is locked during the time PHP serves a request.
包含会话信息的文件在 PHP 处理请求期间被锁定。
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.
因此,如果您有一个当前正在执行的 PHP 脚本(使用会话),并且同一个用户发送了另一个请求,则第二个请求将等到第一个请求完成。

