Page 1 of 1
How does php handle the mutiple http requests simutaniously
Posted: Mon Feb 02, 2009 2:45 pm
by glen09
I'm new to php and from Java world.
What should we do to ensure php thread safe? Configuring php engine? or Following certain coding rules?
Re: How does php handle the mutiple http requests simutaniously
Posted: Mon Feb 02, 2009 2:46 pm
by s.dot
PHP is not a multi-threaded language, though you can hack it to somewhat simulate it.
Re: How does php handle the mutiple http requests simutaniously
Posted: Mon Feb 02, 2009 4:40 pm
by Christopher
PHP runs inside the web server, so the web serve takes care of processes. In general PHP is threadsafe. The core certainly is and most common libraries ... but some are not. So you can run your web server threaded and should be fine, but may have problems.
That answer is the basics, but you need to wrap your head around PHP's "Share Nothing" architecture. No application server, each request reexecutes the script, everything has to be reloaded each request. It sounds horrible, huh? But apparently it works ok...

Re: How does php handle the mutiple http requests simutaniously
Posted: Mon Feb 02, 2009 5:58 pm
by glen09
I have a login.php script. It works fines with one user login. I'm not sure how it handles the multiple user login requests
at the same time. I think there might be some collisions among the multiple requests. What is the best practice to avoid
such collision when coding the php scripts? Or we should let php engine to deal the thread issue?
Re: How does php handle the mutiple http requests simutaniously
Posted: Mon Feb 02, 2009 6:02 pm
by Eran
Read arborint's post again in detail, he explained it very well. Different requests from different clients are ran in different instances of PHP invoked by the web-server, and those instances share nothing in memory or resources. Collisions could only happen on persisted data such as files or databases. Most databases handle such collisions very well, and with files you have to be more careful.
Re: How does php handle the mutiple http requests simutaniously
Posted: Mon Feb 02, 2009 6:16 pm
by Christopher
glen09 wrote:I have a login.php script. It works fines with one user login. I'm not sure how it handles the multiple user login requests
at the same time. I think there might be some collisions among the multiple requests.
Imagine a world where you don't have to worry about such things!
glen09 wrote:What is the best practice to avoidsuch collision when coding the php scripts?
let PHP and the webserver deal the thread issue
glen09 wrote:Or we should let php engine to deal the thread issue?
Yes
Re: How does php handle the mutiple http requests simutaniously
Posted: Tue Feb 03, 2009 9:36 am
by josh
Don't rely on concurrent access to shared resources that can't handle transactions. For instance use a transactional database not a flat file.
Re: How does php handle the mutiple http requests simutaniously
Posted: Tue Feb 03, 2009 11:55 am
by Christopher
jshpro2 wrote:Don't rely on concurrent access to shared resources that can't handle transactions. For instance use a transactional database not a flat file.
Which is the essence of PHP's Share Nothing style which is to use standard subsystems like databases, LDAP, filesystems, etc. that are highly optimized for specific jobs rather than language libraries. So for multiple requests--depend on Apache; for transactions--depend on a database; etc.
Re: How does php handle the mutiple http requests simutaniously
Posted: Tue Feb 03, 2009 5:18 pm
by pickle
Imagine each request opens and runs a copy of your PHP file. There's no problem with 10 copies of the same file running at the same time - they each run independently.
A problem may occur if you have a script that reads & writes to a text file, or some other resource without concurrency handling. Databases handle multiple requests just fine, so you don't need to worry about them either.