Why cannot Apache handle multiple requests at the same time?
Asked Answered
G

9

17

I have AMPPS installed.

My Apache server cannot handle multiple php requests at once (for example if I call localhost/script.php multiple times, they are processed in a consecutive order). script.php consists only of <?php sleep(10); ?>.

I read that MaxClients directive is responsible for concurrent access configuration, but it is missing in my httpd.conf at all.

Disabling Xdebug and writing session_write_close(); to the beginning of the script didn't work.

When I added session_start(); to the beginning of the file and my code looked like:

<?php

session_start();
session_write_close();
sleep(10);
phpinfo();

echo "Done";

When making 5 requests to localhost/script.php, last 4 waited for the first one to end and then ended concurrently.

Please, help me resolve the issue. If any information that is needed to help me resolve this problem is missing, please notify and I will add it.

Glint answered 25/5, 2016 at 8:15 Comment(6)
try this: #3389996Jedlicka
session_write_close() should prevent session-related waits but... are sessions required to reproduce the issue? Or, in other words, does it work as expected if you remove session_start()? Your question suggests sessions are not required at all but I wonder why you mention then.Expiration
without session_start() code does not execute concurrently at all, requests are handled one at a time. with session_start(), first request is handled before others, and then the rest of requests are executed concurrently.Glint
i mentioned them because someone suggested that it might be session-related issue.Glint
Are you able to resolve this? It seems like I have a similar kind of issue. I am using php 7 with (xampp 3.2.2).Bespread
I removed the session from my code which probably was locking files and does not allow other scripts to be executed unless the previous file was unlocked. It worked as expected.Bespread
B
35

Apache can surely handle multiple requests at the same time, there is something surely going wrong within your apache configuration.

It depends on which version of Apache you are using and how it is configured, but a common default configuration uses multiple workers with multiple threads to handle simultaneous requests. See http://httpd.apache.org/docs/2.2/mod/worker.html for a rundown of how this works.

The reason why you are facing it is: There is some lock somewhere - which can happen, for instance, if the two requests come from the same client, and you are using file-based sessions in PHP : while a script is being executed, the session is "locked", which means the server/client will have to wait until the first request is finished (and the file unlocked) to be able to use the file to open the session for the second user.

The requests come from the same client AND the same browser; most browsers will queue the requests in this case, even when there is nothing server-side producing this behaviour.

Brute answered 8/6, 2016 at 13:59 Comment(3)
I went through the document and configured MPM but still not working. session_write_close() works for 6 requests at once. Next 6 requests are processed after previous 6 done.Shrub
I was using codeigniter frame work for my application & i tried to run same url (to import some data) in 3 tabs of same browser thinking that it will run simultaneously but it didn't. 2nd & 3rd requests are waiting for the first one to complete. So after reading this, i tried to run it on 3 different browser thinking it might be the session related lock & got it run simultaneously. Thanks!Stanwinn
Is this possible to provide some code? I tried it but it did not work. @Stanwinn I have the same things to work. In the first tab, I am running an import data script and in the second tab, I want to see the progress from the database log table. But it only load when the import data script finished.Goebel
S
7

Probably becouse of sessions locking. When you don't need to edit session variables, close it.

http://php.net/manual/en/function.session-write-close.php

Schifra answered 25/5, 2016 at 8:20 Comment(1)
Maybe check this: https://mcmap.net/q/743174/-how-does-session_start-lock-in-php So, does this problem affects other browsers too?Schifra
L
5

manipulate yours sessions, write on start of script.php

// manipulate writes, and unlock session file!
session_start();
$_SESSION['admin'] = 1;
$_SESSION['user'] = 'Username';
session_write_close(); // unlock session file, to another script can access

// start your script without php session block
sleep(30); 
echo $_SESSION['user'];

// another script can run without wait this script finish
Leonorleonora answered 14/4, 2018 at 13:41 Comment(1)
This is exactly what I needed. I am streaming a file with PHP and the streaming request was blocking me from making another request with the same user. Turns out, sessions were blocking me from making another request.Story
S
0

Have you tried to make the simultaneous calls with different browser tabs/windows/instances ?

Apache is multithreaded, so, it definitely can handle your parallel requests. It seems you have some things to check:

  • Make requests with an appropriate client for testing (like apache benchmark) - Take a look at https://httpd.apache.org/docs/2.4/programs/ab.html

  • Check your setup on apache. There is some wrong setups that can produce strange behavior, like single request at a time. Take a look at fork and worker parameters at httpd.conf. Suggestion: use all default parameters for testing.

Stepfather answered 8/6, 2016 at 5:24 Comment(0)
T
0

Apache provides a variety of multi-processing modules (Apache calls these MPMs) that dictate how client requests are handled. Basically, this allows administrators to swap out its connection handling architecture easily. These are:

  1. mpm_prefork: This processing module spawns processes with a single thread each to handle request. Each child can handle a single connection at a time.
  2. mpm_worker: This module spawns processes that can each manage multiple threads. Each of these threads can handle a single connection.Since there are more threads than processes, this also means that new connections can immediately take a free thread instead of having to wait for a free process.
  3. mpm_event: This module is similar to the worker module in most situations, but is optimized to handle keep-alive connections. When using the worker MPM, a connection will hold a thread regardless of whether a request is actively being made for as long as the connection is kept alive.
Tother answered 9/6, 2016 at 11:28 Comment(0)
E
0

Try including the sleep and phpinfo within the session before calling session close. As it looks like the sessions(all the five are treated as same and are terminated with the first one being terminated). Maybe verify if the Session Ids are the same. By keeping the session open you can see that concurrently they are handled.

Ennoble answered 9/6, 2016 at 21:48 Comment(0)
C
0

I encountered a similar problem. Multiple requests kept hanging randomly while connecting to the server.

Tried changing the mpm configurations, with no use.

Finally this one seems to solve the problem for me. (from https://serverfault.com/a/680075)

AcceptFilter http none
EnableSendfile Off 
EnableMMAP off 
Chew answered 15/7, 2018 at 17:39 Comment(0)
M
0

Artisan is single threaded! just do not serve your project with artisan. Instead make a route to your laravel/public Or you can use Laravel Octan, which is really powerful.

Monreal answered 28/4, 2023 at 22:49 Comment(0)
H
-1

You can move session storing from files to database - than you would have possibility to request your files all at once without waiting - or - if you don't need session in your script turn it off (don't use session_start();)

Homerus answered 25/5, 2016 at 8:37 Comment(1)
I haven't used in the first place. As I said, I don't have anything in yhe file except sleep(10)Glint

© 2022 - 2024 — McMap. All rights reserved.