How does php handle the mutiple http requests simutaniously

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
glen09
Forum Newbie
Posts: 6
Joined: Mon Feb 02, 2009 2:37 pm

How does php handle the mutiple http requests simutaniously

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How does php handle the mutiple http requests simutaniously

Post by s.dot »

PHP is not a multi-threaded language, though you can hack it to somewhat simulate it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How does php handle the mutiple http requests simutaniously

Post 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... ;)
(#10850)
glen09
Forum Newbie
Posts: 6
Joined: Mon Feb 02, 2009 2:37 pm

Re: How does php handle the mutiple http requests simutaniously

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: How does php handle the mutiple http requests simutaniously

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How does php handle the mutiple http requests simutaniously

Post 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
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: How does php handle the mutiple http requests simutaniously

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How does php handle the mutiple http requests simutaniously

Post 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.
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How does php handle the mutiple http requests simutaniously

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply