Hi my question is how the data flow works exactly in Apache web server + PHP.
When user access to url: localhost/index.php and in same time when another user access the same url i guess requests are executes one by one (not multi-threaded) and the first user get response then other one.
the question is: if somehow the first request stay in loop for long time like 1min the other users should wait the first request to finish then their requests to be finished in order to get response from Apache web server + PHP? if answer is yes (other users should wait in queue): can we make requests to be executed in parallel (multi-thread) in order to prevent waiting in queue
Whilst PHP may be single threaded, Apache can run multi-process and multi-threaded. This allows many requests to be executed simultaneously. You can configure how many simultaneous requests in fact.
You can actually see Apache serving these requests live, see which are waiting, and which are being served via mod_status (http://httpd.apache.org/docs/2.2/mod/mod_status.html).
Apache has multiple threads so multiple PHP scripts can run at once.
Webservers such as Nginx use an event driven architecture. Asynchronous I/O and all that jazz.
Webservers usually have some sort of mechanism to enable them to handle more than 1 request at a time, or at least make it seem like they can.
Apache uses threads, Nginx uses an event loop, but they are not serial in the sense that you want. The reason is very simple: it wastes resources. While your PHP script is waiting for the harddisk to move to the right position so you can read a block from a file you're reading you might as well be doing something else, ie. handle another request that doesn't require I/O right now.
If it's crucial some request finishes before other users make requests you should consider switching to a more decoupled asynchronous architecture. I don't know how invested you are in your current solution. Event handling can be implemented as simply as a simple poll or even a long poll.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With