Programming to take advantage of multi core processors with PHP

Matt Graham danceswithcrows at usa.net
Thu Mar 3 10:42:53 MST 2011


From: keith smith <klsmith2020 at yahoo.com>
> I was reading on computers and phones having multiple cores and
> programming for multiple cores - http://www.linux-mag.com/id/8274/ 
> The 2nd to the last paragraph starts with "If you write software,
> you should be paying close attention to this trend." 
> That got me to thinking about how to do so with PHP.  
> Anyone writing PHP code to take advantage of multiple cores?  Or
> is this even necessary?

If you're using PHP as it's usually used (running under the apache prefork
model as a provider of dynamically generated web pages, with a DB of some sort
as a backend), then you have less to do than you might think.  Each apache
instance is a separate process.  So if you have 10 apache processes running,
you could use up to 10 CPUs at the same time.  And you're generally storing
persistent data in the DB, or in sessions, or with memcached, so those
generally handle the annoying locking/race problems with multithreaded apps.

If you're using PHP on the CLI, then things get a little more complex.  The
usual way to deal with multiprocessor things is to have one master process,
which fork()/exec()s several slave processes, which do all the actual work. 
Work units are assigned to the slave processes using whatever you want.  You
have to handle locking of shared resources yourself, but using a DB can make
that a bit easier.

More specifics about what it is you're doing would probably lead to better and
more concrete advice.

-- 
Matt G / Dances With Crows
The Crow202 Blog:  http://crow202.org/wordpress/
There is no Darkness in Eternity/But only Light too dim for us to see



More information about the PLUG-discuss mailing list