PHP超时会阻止同一网络加载页面上的人吗? [英] Do PHP timeouts stop people on the same network loading pages?

查看:128
本文介绍了PHP超时会阻止同一网络加载页面上的人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的PHP页面正在执行需要很长时间的任务,并且我尝试同时从同一站点加载另一个页面,那么在第一页超时之前,该页面将不会加载。例如,如果我的超时设置为60秒,那么我将无法在需要很长时间加载/超时的页面后60秒加载任何其他页面。据我所知这是预期的行为。

If I have a PHP page that is doing a task that takes a long time, and I try to load another page from the same site at the same time, that page won't load until the first page has timed out. For instance if my timeout was set to 60 seconds, then I wouldn't be able to load any other page until 60 seconds after the page that was taking a long time to load/timeout. As far as I know this is expected behaviour.

我想弄清楚的是,创建上述情况的错误/长时间加载PHP脚本是否也会影响其他人们在同一个网络上。我个人认为这是一个浏览器问题(即如果我加载 http://somesite.com/myscript.php在chrome中它开始在后台运行它的魔力,我无法加载 http://somesite.com/ myscript2.php 直到超时,但我可以在Firefox中加载该页面。但是,我听到了相互矛盾的陈述,说超时会发生在同一网络上的每个人(IP地址?)。

What I am trying to figure out is whether an erroneous/long loading PHP script that creates the above situation would also affect other people on the same network. I personally thought it was a browser issues (i.e. if I loaded http://somesite.com/myscript.php in chrome and it start working it's magic in the background, I couldn't then load http://somesite.com/myscript2.php until that had timed out, but I could load that page in Firefox). However, I've heard contradictory statements, saying that the timeout would happen to everyone on the same network (IP address?).

我的脚本适用于从中导入的一些数据sage并且需要相当长的时间才能运行 - 它可以在它完成之前超时(例如,如果sage导入在一周内崩溃),所以我再次运行它并从它停止的地方开始。我担心办公室里的其他工作人员在运行时将无法访问该网站。

My script works on some data imported from sage and takes quite a long time to run - sometiems it can timeout before it finishes (i.e. if the sage import crashes over the weeked), so I run it again and it picks up where it left off. I am worried that other staff in the office will not be able to access the site while this is running.

推荐答案

问题你这里实际上与(我猜)您使用的是会话。这可能有点延伸,但它会说明你所描述的内容。

The problem you have here is actually related to the fact that (I'm guessing) you are using sessions. This may be a bit of a stretch, but it would account for exactly what you describe.

除非您的网络服务器已设置,否则这实际上不是预期行为使用单个线程运行单个进程,我非常怀疑。这将创建一种情况,即Web服务器在任何时候都只能处理单个请求,这会影响网络上的每个人。这正是为什么您的Web服务器可能不会像这样设置的原因 - 事实上我怀疑您会发现无法像这样配置您的服务器,因为它会使服务器有点无用。在一些聪明的亚力克与Node.js怎么样?之前。 - 这是一个特例,因为我相信你已经很清楚了。

This is not in fact "expected behaviour" unless your web server is set up to run a single process with a single thread, which I highly doubt. This would create a situation where the web server is only able to handle a single request at any one time, and this would affect everybody on the network. This is exactly why your web server probably won't be set up like this - in fact I suspect you will find it is impossible to configure your server like this, as it would make the server somewhat useless. And before some smart alec chimes in with "what about Node.js?" - that is a special case, as I am sure you are already well aware.

当一个PHP脚本打开一个会话时,它对该文件有一个独占锁定其中存储了会话数据。这意味着任何后续请求都会在调用 session_start()时阻塞,而PHP会尝试获取会话数据文件的独占锁定 - 这是不可能的,因为你的先前的请求仍有一个。一旦您的上一个请求完成,它就会释放它对文件的锁定,并且下一个请求就能完成。由于会话是每台机器(实际上是每次浏览会话,顾名思义,这就是它在不同浏览器中工作的原因),这不会影响网络的其他用户,而是会离开您的网站设置,这是一个问题,即使只是为了你是不好的做法,很容易避免。

When a PHP script has a session open, it has an exclusive lock on the file in which the session data is stored. This means that any subsequent request will block at the call to session_start() while PHP tries to acquire that exclusive lock on the session data file - which it can't, because your previous request still has one. As soon as your previous request finishes, it releases it's lock on the file and the next request is able to complete. Since sessions are per-machine (in fact per-browsing session, as the name suggests, which is why it works in a different browser) this will not affect other users of your network, but leaving your site set up so that this is an issue even just for you is bad practice and easily avoidable.

解决方法是致电 session_write_close() 。这会导致脚本关闭会话文件并释放它的锁定。您应该尝试在开始长时间运行的过程之前完成会话数据,或者在完成之前不要调用 session_start()

理论上你可以调用 session_write_close(),然后再次调用 session_start()在脚本中,但我发现PHP在这方面有时表现出错误行为(我认为这与cookie有关,但不要引用我的话)。显然,请注意设置cookie修改标题的事实,因此您必须在输出任何数据或启用输出缓冲之前调用 session_start()

In theory you can call session_write_close() and then call session_start() again later in the script, but I have found that PHP sometimes exhibits buggy behaviour in this respect (I think this is cookie related, but don't quote me on that). Obviously, pay attention to the fact the setting cookies modifies the headers, so you have to call session_start() before you output any data or enable output buffering.

例如,考虑这个脚本:

<?php

  session_start();

  if (!isset($_SESSION['someval'])) {
    $_SESSION['someval'] = 1;
  } else {
    $_SESSION['someval']++;
  }

  echo "someval is {$_SESSION['someval']}";

  sleep(10);

使用上面的脚本,您需要等待10秒才能再发出第二个请求。但是,如果您在 echo 行之后添加对 session_write_close()的调用,您将能够再次发出请求在上一个请求完成之前。

With the above script, you will have to wait 10 seconds before you are able to make a second request. However, if you add a call to session_write_close() after the echo line, you will be able to make another request before the previous request has completed.

这篇关于PHP超时会阻止同一网络加载页面上的人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆