I maintain a custom built CMS-like application.
Whenever a document is submitted, several tasks are performed that can be roughly grouped into the following categories:
Category 1 includes updates to various MySQL tables relating to a document's content.
Category 2 includes parsing of HTML content stored in MySQL LONGTEXT fields to perform some automatic anchor tag transformations. I suspect that a great deal of computation time is spent in this task.
Category 3 includes updates to a simple MySQL-based search index using just a handful of fields corresponding to the document.
All of these tasks need to complete for the document submission to be considered complete.
The machine that hosts this application has dual quad-core Xeon processors (a total of 8 cores). However, whenever a document submits, all PHP code that executes is constrained to a single process running on one of the cores.
My question:
What schemes, if any, have you used to split up your PHP/MySQL web application processing load among multiple CPU cores? My ideal solution would basically spawn a few processes, let them execute in parallel on several cores, and then block until all of the processes are done.
Related question:
What is your favorite PHP performance profiling tool?
PHP is not quite oriented towards multi-threading : as you already noticed, each page is served by one PHP process -- that does one thing at a time, including just "waiting" while an SQL query is executed on the database server.
There is not much you can do about that, unfortunately : it's the way PHP works.
Still, here's a couple of thoughts :
So, in fact, your server's 8 core will end up being used ;-)
And, if you think your pages are taking too long to generate, a possible solution is to separate your calculations in two groups :
For the kind of situations in my second point, as you don't need those things done immediately... Well, just don't do them immediately ;-)
A solution that I often use is some queuing mechanism :
And for some other manipulations, you just want them run every X minutes -- and, here too, a cronjob is the perfect tool.