开发者

Hundreds of Apache Processes Hanging around after Ajax Requests when Session Variable is Used

开发者 https://www.devze.com 2023-03-29 00:59 出处:网络
After a PHP web page from an Apache server is loaded by the browser, client side javascript is sending AJAX requests every 5 seconds to a PHP update script. The AJAX response, encoded开发者_如何学Go i

After a PHP web page from an Apache server is loaded by the browser, client side javascript is sending AJAX requests every 5 seconds to a PHP update script. The AJAX response, encoded开发者_如何学Go in JSON, can be quite large, so I want to send simply the empty array '[]' if there is no change since the previous AJAX request, or the entire array containing various JSON objects if there has been a change since the last AJAX requests.

To implement sending '[]' or the full array I'm using a session variable. My update script looks like:

<?php
session_start();
$output = '[]'; // server is running PHP 5.1 so can't use built in JSON encode
// code here queries a database and updates $output based on the query result
if (isset($_SESSION['previous_output'])) {
    if ($_SESSION['previous_output'] == $output) {
        echo '[]';
    } else {
        $_SESSION['previous_output'] = $output;
        echo $output;
    }
}
?>

After implementing the above script, the web server administrator noticed "too many" httpd processes being created and hanging around, eventually bringing the server to a grinding halt.

When I remove the code involving the session variables, some httpd processes still hang around, but the "backlog" is not as bad, and eventually the processes go away before there is noticeable impact on the server.

The web server is not heavily used, serving maybe half a dozen "visitors' at any time. The web page doing the 5 second AJAX requests is "protected", meaning only a single admin user can access it at a time.

I found another posting at PHP MySQLi Singleton for Ajax-Requests end in to many processes that sounds similar, and it refers to the AJAX Push Engine (APE) project, which I would like to investigate further eventually. However, I am currently pressed for time and resources (e.g. cannot setup an APE server...), so I am at a lost as to what can be done.

Any advice on how to do the 5 second AJAX requests without adversely impacting server performance? Why would the use of the session variable make such a noticeable difference?


Apache tries to maintain a set of idle workers to process incoming requests. These settings can be tweaked (min number of idle workers, max number of workers - of particular interest MaxRequestsPerChild, try lowering this value). This shouldn't cause a problem - assuming your script is terminating correctly (this shouldn't be an issue for the typical web app). You might get a thorough answer from the serverfault community.

Also, depending on the server load (# of active users, resources the api uses, etc.), you might need to scale you app (research autoscaling with amazon's ec2 service).

http://httpd.apache.org/docs/2.0/mod/worker.html

0

精彩评论

暂无评论...
验证码 换一张
取 消