Do PHP timeouts stop people on the same network loading pages?

后端 未结 2 1428
逝去的感伤
逝去的感伤 2021-01-20 05:17

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 p

相关标签:
2条回答
  • 2021-01-20 05:43

    Hmm... I did not check but I think that each request to the webserver is handled in a thread of its own. Thereby a different request should not be blocked. Just try :-) Use a different browser and access your page while the big script is running!

    Err.. I just see that this worked for you :-) And it should for others, too.

    0 讨论(0)
  • 2021-01-20 05:49

    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.

    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.

    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.

    The solution to this is to call session_write_close() as soon as you have finished with the session data in a given script. This causes the script to close the session file and release it's lock. You should try and either finish with the session data before you start the long running process, or not call session_start() until after it has completed.

    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.

    For example, consider this script:

    <?php
    
      session_start();
    
      if (!isset($_SESSION['someval'])) {
        $_SESSION['someval'] = 1;
      } else {
        $_SESSION['someval']++;
      }
    
      echo "someval is {$_SESSION['someval']}";
    
      sleep(10);
    

    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.

    0 讨论(0)
提交回复
热议问题