Getting Started
Introduction
PHP Multi-process is a combination of scripts and hacks to enable more-asynchronous[1] processing in php. PHP Multi-process allows a php script (the parent) to spawn an unlimited[2] number of independent child processes. The parent continues to execute until all children processes are complete, or until the parent script times out. The children execute independently of each-other and of the parent, allowing all of the children to execute at the same time. The goal of this is to break down large processor intensive tasks, making them practical for real-time usage.
Server requirements
- PHP running on an Apache[3] web server
- Ability to use exec() and execute php through command line (PHP-CLI)
- Sqlite or MySQL (for caching)
Using PHP Multi-process
- Upload and extract the most recent version of PHP Multi-process to your server.
- Create a writable directory named sqlite (if you do not already have one). This is where the sqlite cache database will exist.
- Determine what code you need to process and break it up into independent sections.
/*-----------------BEGIN YOUR OWN CODE HERE------------------------*/ /* This is a normal PHP script so you can do whatever you want. But, don't output anything because this is running in the background. Variables you passed are available through $variables = $mp->getVariables(); */ /*-----------------END YOUR OWN CODE HERE---------------------------*/
For each section of code, place it into the child_proc.php, and name the file something unique. You will end up with several files with names like: process_1.php, process_2.php, etc… Upload these to your server.
- Edit the multi_proc.php script, and put the process names that you just created into the $processes array. You may need to put the path of the file rather than just the file name if you upload these files to a different directory than the multi_proc.php file.
$processes[] = array('path'=>'process_1.php', 'variables'=>array('some_value'=>5, 'another_value'=>10) ); $processes[] = array('path'=>'process_2.php', 'variables'=>array('other_value'=>7, 'get'=>$_GET) ); $mp = new multi_process(); $mp->createChildren($processes);
- Lastly, point your browser to the multi_proc.php script.
Be sure to do some testing on your specific setup to ensure each child is processing correctly. It may take some light debugging to get everything to work correctly, as different servers behave slightly different when using exec and other command line scripts.
Download
Repository
http://github.com/jestep/PHP-Multi-process/tree
History
Current version
v1.1.1 (Beta)
Previous versions
v1.1 (Beta)
v1.0 (Alpha Release)
Notes
Asynchronous
PHP cannot process in a truly Asynchronous manner. This script is not actually Asynchronous, but if used in a controlled manner will emulate Asynchronous execution. In the end, the script’s ability to successfully process is very dependent on the server’s set parameters, including execution time.
Unlimited
On the code/application level, the number of children is unlimited. However, insufficient system resources could cause undesirable and possibly-uncontrolled results. Currently there is not any mechanism to check system resources.
Benchmarking
In benchmarking tests, PHP Multi-process delivers the desired, and mostly linear reduction in execution time. Assuming that a script can be divided into children with identical processes to execute, PHP Multi-process will reduce the overall execution time by a factor of the number of children.Again, this could be dependent on available system resources, and whether each child has the same amount of processing to complete.
Windows
This script could theoretically work in an Apache Windows environment as well, but paths and the exec command would almost certainly need to be modified in order for this to work. It is not tested in Windows, but there’s nothing in PHP Multi-process that would inherently be incompatible with Windows.
Inspiration
There are a number of ways to do similar multi-processing tasks with php, pcntl_fork being the most common that I’ve come across. None 0f the methods available work with Apache PHP (the most commonly method of running PHP). Needing to run some massive reporting scripts using large datasets, and not being willing to compromise my PHP installations this script is the product of many hours of brainstorming, testing, and frusteration.
As far as I know, it is the first / only public script like this in existence. However, it is by no means perfect, and for public usage may not be production ready. If you would like to help develop this script, or have made your own modifications, please feel free to send them to me. I would definitely like to further improve and develop PHP Multi-process, so comments, suggestion, and criticism are greatly appreciated.