session_start似乎非常慢(但有时)

[英]session_start seems to be very slow (but only sometimes)


For some odd reason, just today our server decided to be very slow during the starting of sessions. For every session_start, the server either times out after 30 seconds, or it'll take about 20 seconds for it to start the session. This is very weird, seeing as it hasn't done this for a very long time (the last time our server did this was about 7 months ago). I've tried to change the session to run through a database instead, and that works fine, however, as our current website is built, it'd take days to go on every page and change the loading of sessions to include a new session handler. Therefore my question remains:

由于一些奇怪的原因,就在今天,我们的服务器决定在会话开始时非常慢。对于每个session_start,服务器要么在30秒后超时,要么大约需要20秒才能启动会话。这很奇怪,因为它已经很久没有这样做了(上次我们的服务器是在7个月前完成的)。我试着改变会话来运行数据库,但是这样做很好,但是,当我们当前的网站被构建时,每一页都要花上几天的时间,并且改变会话的加载,包括一个新的会话处理程序。所以我的问题是:

Why is it so slow, and why only sometimes?

为什么它这么慢,为什么只是有时?

We run on a dedicated hetzner server with 24GB's of ram, and a CPU fast enough to just run a simple webserver (a Xeon, I believe, but I'm not sure). We run debian on the server with an apache+fastcgi+php5 setup.

我们运行在一个专门的hetzner服务器上,它有24GB的ram, CPU的速度足以运行一个简单的web服务器(我相信是Xeon,但我不确定)。我们使用apache+fastcgi+php5设置在服务器上运行debian。

The server doesn't report much load, neither through server-status as well as the top command. Vnstat reports no problem whatsoever with our network link (again, that wouldn't result in a slow local session handling). IOtop reports no problem with processes taking over the entire harddrive. Writing to the tmp folder where the session files are located works fast if done through vim.

服务器没有报告太多负载,也没有通过服务器状态和最高命令报告。Vnstat报告我们的网络链接没有任何问题(同样,这不会导致本地会话处理缓慢)。IOtop报告说接管整个harddrive的过程没有问题。如果通过vim编写到会话文件所在的tmp文件夹,则工作速度很快。

Again, to make this clear, my main concern here isn't whether or not we should switch to a DB or a memory-cached version of the sessions, it's simply to ask why this happens, because everything I take a look at seems to be working fine, except for the PHP itself.

再说一遍,这里我主要关心的不是我们是否应该切换到DB或session的内存缓存版本,而是问为什么会发生这种情况,因为我看到的所有东西似乎都运行得很好,除了PHP本身。

EDIT: The maximum file in our PHP tmp directory is 2.9 MB, so nothing that should make an impact, I believe.

编辑:我们的PHP tmp目录中的最大文件是2.9 MB,所以我认为不会有什么影响。

UPDATE: I did never figure out what was wrong and/or how to fix it, but the problem disappeared after we switched over to memcached/db sessions.

更新:我从来没有弄清楚哪里出错了,以及/或如何修复它,但是当我们切换到memcached/db会话后,问题就消失了。

6 个解决方案

#1


14  

Have you tried session_write_close(); ? This will disable write-ability in session variables but you can still read data from them. And later when you need to write a session variable, reopen it.

你有试过session_write_close();吗?这将禁用会话变量的写能力,但您仍然可以从它们读取数据。稍后当您需要编写会话变量时,重新打开它。

I have also suffered from this problem but this thing worked like a charm. This is what i do:

我也遇到过这个问题,但是这个东西很好用。这就是我所做的:

session_start(); //starts the session
$_SESSION['user']="Me";
session_write_close();   // close write capability
echo $_SESSION['user']; // you can still access it

#2


4  

I had the same problem: suddenly the server took 30 seconds to execute a request. I noticed it was because of session_start(). The first request was fast, but each next request took some 30 sec to be executed. I found that the session file in c:\wamp\tmp was locked by the first request for some 30 sec. During this time the second request was waiting for the file to be unlocked. I found out it had something to do with rewrite_mod and .htaccess. I disabled rewrite_mod and commented out every line in .htaccess and it works again like a charm. I don't know why this happend because I don't remember change any settings or conf on wamp.

我遇到了同样的问题:服务器突然花了30秒来执行请求。我注意到这是因为session_start()。第一个请求速度很快,但每个下一个请求都需要大约30秒才能执行。我发现c:\wamp\tmp中的会话文件被第一个请求锁定了大约30秒。在此期间,第二个请求正在等待文件解锁。我发现它与rewrite_mod和。htaccess有关。我禁用了rewrite_mod,并注释了。htaccess中的每一行,它又像一个魔咒一样工作了。我不知道为什么会这样,因为我不记得在wamp上更改任何设置或conf。

#3


1  

I ran into this problem too. It was answered here:

我也遇到了这个问题。这里是回答:

Problem with function session_start() (works slowly)

函数session_start()的问题(工作缓慢)

Sessions are locked by PHP while one script is executing, so if scripts are stacked under the same session, they can cause these surprisingly long delays.

当一个脚本正在执行时,会话被PHP锁定,所以如果脚本被堆放在同一个会话下,它们会导致令人惊讶的长时间延迟。

#4


0  

Each session is stored by apache as a text file.

apache将每个会话存储为一个文本文件。

When session start is use to resume an existing session (via cookie identifier for example) maybe a big session file (a Session with a lot of content inside) can be slow to be started?

当session start用于恢复一个现有会话(例如通过cookie标识符)时,一个大的会话文件(一个包含很多内容的会话)是否会启动缓慢?

If this is the case probably you application is putting to much data into sessions.

如果是这样,那么您的应用程序可能将大量数据放入会话中。

#5


0  

Please check if you have correct memcache settings e.g. in /etc/php.d/memcached.ini

请检查是否有正确的memcache设置,例如:in /etc/ph .d/ cache .ini

#6


0  

I know this is an old question but I've just fixed this issue on my server. All I did was turn on the bypass cache for the domains in the cache manager in the cpanel.

我知道这是个老问题,但我刚刚在服务器上解决了这个问题。我所做的就是在cpanel中为缓存管理器中的域打开旁路缓存。

My sessions were taking ages to start and close now they are instant.

我的疗程开始和结束都花了很长时间,现在都是即时的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/04/30/6fb6055e843c39fab7ba49b8a399ebc6.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号