net_bazzline / php_component_process_fork_manager
free as in freedom php component process fork manager for easy up process forking and observing threads
Requires
- php: >=5.3.3
- net_bazzline/php_component_memory_limit_manager: 1.1.*
- net_bazzline/php_component_time_limit_manager: 1.0.*
- symfony/event-dispatcher: ~2.7||~3.0
Requires (Dev)
- mockery/mockery: 0.9.5
- phpunit/phpunit: ~4.8||~5.5
README
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:
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.
- basic
- with logging by using event dispatcher
- with more tasks then open threads
- with reaching memory limit
- with reaching time limit
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
With Packagist
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
- 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?
- 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
- updated example section
- 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.
- fork explained at wikipedia.org
- fork tutorial for linux
- php process forking
- php forking tutorial
- php forking example to speed up image resizing
- pcntl_fork manual
- php-process-manager by dannymar
- php spork by gwilym
- php fork by mitallast
- workman by jimbosjsb
- PHP-Fork by robbmj
- php-fork by mpierzchalski
- php spork by kriswallsmith
- PHP_Fork by pear
- process-fork by pbergman
- forki by kakawait
- php semaphore fork test by johan addriaans
- fork-helper by ducan3dc
- php thread by mmarquez
- forkdaemon php by baracudanetworks
- power spawn by lordgnu
- phpdaemon by yutas
- phpdaemon by Ocean Internet
- PHPDaemonizer
- PHP piped process communications
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 :-].