2

I have apache as http server and php, i sent about 5 simultaneous requests from browser to this php script which has a for loop, which takes long time to complete, I saw all the concurrent requests are blocked and served sequentially.

How to configure apache or php for non blocking behavior of http request serving instead of sequentially serving behavior?

    I know that Tomcat server http connector can tune using following server parameters

    – Threads (maxThreads)
    – Keep alive requests (maxKeepAliveRequests)
    – TCP Backlog (acceptCount)
    – connectionTimeout
    – Socket buffers
    - Use different connectors (nio, apr, bio)

etc...

the php code snippet which was tested for 5 concurrent requests, but served sequentially by the web server (apache 2.2, php 5.3)

<?php
for ($i = 1; $i < 500000; $i++) { //do some processing which takes some time
    $sq = sqrt($i); 
    $val += $sq;
}
echo $val;
?>
Buddhi
  • 2,224
  • 5
  • 32
  • 43
  • 1
    Don't know exactly, what you are trying to do, but I guess you should use a cronjob, or a dedicated job server (like gearman) instead. – KingCrunch Jul 31 '12 at 12:07
  • Actually I want requests to be served concurrently, not sequentially – Buddhi Jul 31 '12 at 12:09
  • 2
    I see, but the question is: Why? Long-running scripts should not get called from within an browsers (usually; There are exceptions of course). For example you can trigger a job (--> push to gearman) with the browser and show the status of this job on a different page. – KingCrunch Jul 31 '12 at 12:21

3 Answers3

5

I ran across a similar issue and discovered that Apache and PHP were configured correctly, however my PHP scripts were blocking on a particular resource. In my case, it was the session file that was getting locked and each request needed to complete before the session file became free again and could serve a new request. (Here is an article that explains the issue in more depth: http://konrness.com/php5/how-to-prevent-blocking-php-requests/ ) Once I prevented the session from blocking, I started to see the parallelism one would expect.

Evan
  • 141
  • 4
2

Use following directives in apache.conf

StartServers          3
MinSpareServers       5
MaxSpareServers       7

It will spawn additional processes waiting for concurrent connections to serve them instantly. Adjust the numbers per your needs.

mnmnc
  • 374
  • 3
  • 14
1

i tested requests in this set up using a for loop in php script

You created a script which makes sequential requests for content via HTTP and found that they were served sequentially?

You've not shown your code. Although it would be possible to implement an event-based socket client in a single thread, this would be rather difficult to code. Even if this were the case, I'd still be surprised if the content did not appear to be processed sequentially, simply because (in the absence of chunking) each message will be a complete request.

I saw all the concurrent requests are blocked

There's lots of places where there are mutexes - on a per-php-session basis, on a per-ssl-session basis are obvious candidates even before you start introducing further ones in your code.

That you don't mention what version of Apache this is, nor, more importantly what mpm engine is being used, nor any mention of how it is configured, nor what operating system this runs on makes me think that your tests were probably not very sophisticated.

Your conclusions explicitly refute a basic requirement for a webserver - that it can handle more than one client concurrently.

How to configure apache or php for non blocking behavior of http request serving

Based on the available evidence your testing/analysis is flawed, it already does this.

symcbean
  • 47,736
  • 6
  • 59
  • 94