Reputation: 1322
This a bit of a theoretical question coming from someone very inexperienced in programming and servers, but here goes.
If I have a PHP file stored on a server, what happens if multiple people start accessing this script at the same time? Do multiple requests stack or can they be processed in parallel? What should I look up to better understand how servers work?
I hope this makes sense!
Upvotes: 15
Views: 6240
Reputation: 3750
With a LEMP stack, most people are using PHP-FPM.
Overview points:
Upvotes: 0
Reputation: 400972
The webserver (Apache, for example) is generally able to deal with several requests at the same time (the default being 200 or 400 for Apache).
If the requests correspond to read-only situations, there should be no problem at all : several process can read the same file at the same time -- and if that file is a PHP script, several process can execute it at the same time.
If your script is query-ing a database, there should not be too much problems : databases are made to deal with concurrency situations (even if reads scale better than writes, which may have to be stacked, if they modify the same data).
If your script is trying to write to a file, you should put some locking mecanism in place (using flock
, for example), to avoid that ; as a consequence, each process will wait until there is no other writing to the file, before writing.
Upvotes: 9
Reputation: 54445
This is pretty much a non-issue, as modern operating systems will happily let multiple processes read a given file at the same time. (i.e.: They're "processed" in parallel.)
However, if a given file needs to be written to by multiple scripts at the same time, then PHP will effectively "stack" the pages that are attempting to gain write access until the file has become available again.
Upvotes: 5