PLEASE NOTE, THIS PROJECT IS NO LONGER BEING MAINTAINED
I still like the idea but there is currently no use case to develop it anymore.
Fork Component for PHP
This free as in freedom component easy up process forking and observing in php.
It is using the memory limit manager as well as the time limit manager and the event dispatcher component as robust base.
The build status of the current master branch is tracked by Travis CI:
The scrutinizer status are:
|
The versioneye status is:
Downloads:
It is also available at openhub.net.
Error Handling
mandatory php function "posix_getpid" is not available
-
uncomment "extension=posix.so' in your php.ini
Examples
If you are running on an unix system, you can use "watch 'ps auxf | grep php'" to validate that threads where started.
Install
By Hand
mkdir -p vendor/net_bazzline/php_component_process_fork_manager
cd vendor/net_bazzline/php_component_process_fork_manager
git clone https://github.com/bazzline/php_component_process_fork_manager
composer require net_bazzline/php_component_process_fork_manager:dev-master
Usage
$factory = \Net\Bazzline\Component\ProcessForkManager\ForkManagerFactory();
$manager = $factory->create();
/ @var \Net\Bazzline\Component\ProcessForkManager\TaskInterface $task */
$task = new \My\Task();
$manager->addTask($task);
$manager->execute();
API
Thanks to apigen, the api is available in the document section or online.
Terms
The ForkManager itself gets multiple tasks attached. While executing, the fork manager keeps track of the current number of threads (working tasks) and time or memory limit.
Depending on situation, the manager will shutdown itself. This can be reached by sending different posix signals (for example kill).
-
ForkManager
* gets tasks attached
* uses sub processes to execute task
* takes care that forking is available
* implements posix signal handling
* taking care of optional runtime limit
* taking care of optional memory limit
* taking care of maximal number of sub processes
* provides method "setUpPOSIXSignalHandling" and "dispatchPOSIXSignal" to implement posix signal handling
-
Thread
* the stupid simple sub process that works on a well defined task
* implements posix signal handling
-
AbstractTask
* implements the unique logic a sub process should execute
* implements
* getProcessId()
* getParentProcessId()
* getRuntime()
* getMemoryUsage()
* provides method "setUpPOSIXSignalHandling" and "dispatchPOSIXSignal" to implement posix signal handling
Future Improvements
-
implement usage of pcntl_wifexited
-
evaluate if proc_open is better
-
evaluate if pthreads
-
add shared memory for intern process calls
* ipc-0
* ipc-1
* fifo-0
* fifo-1
* fifo-2
* shm-0
* shm-1)
-
add load based maximum number of threads like throttleing
-
extend signal handling like here
-
TaskInterface could implement
* getGroupId()
* getUserId()
* setGroupId($groupId)
* setUserId($userId)
* task identifier
-
how to write unit tests for forking itself?
* option one
-
there is a theoretical race condition problem and i have no idea how to solve this in php
* the parent process (fork manager) has process id 123
* a child process (task) gets process id 124
* child gets the signal kill or finished its execution and the process id 124 is available again
* between the usleep of the parent, a new system process gets spawned with the process id 124
* how distinguish if the process with id 124 is the child or a new process?
-
implement usage of php coroutine
History
-
upcomming
-
1.1.0 - released at 28.02.2016
* updated example section
* mv run.php run
* chmod +x run
* moved to psr-4 autoloading
* removed dead api and documentation section
* updated dependencies
-
1.0.11 - released at 18.08.2016
* updated development dependencies
-
1.0.10 - released at 07.03.2016
* updated dependencies
-
1.0.9 - released at 18.12.2015
* updated dependencies
-
1.0.8 - released at 11.12.2015
* updated dependencies
-
1.0.7 - released at 18.11.2015
* updated dependencies
-
1.0.6 - released at 28.08.2015
* updated dependencies
-
1.0.5 - released at 11.07.2015
* updated dependencies
-
1.0.4 - released at 04.07.2015
* removed phpmd
* updated dependencies
-
1.0.3 - released at 29.06.2015
* updated dependencies
-
1.0.2 - released at 08.02.2015
* removed dependency to apigen
-
1.0.1 - released at 31.08.2014
* updated dependencies
-
1.0.0 - released at 05.08.2014
* initial commit with examples, unit tests and api documentation
Links
Following links to projects and pages to easy up stepping into process forking (in php).
Thanks to all the great projects and pages out there.
Final Words
Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it. Donate something if you love it :-].