PHP Classes

Process Fork Manager for PHP: Run parallel processes and manage their execution

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 217 All time: 8,296 This week: 63Up
Version License PHP version Categories
php_component_proces 1.0.12GNU Lesser Genera...5.3PHP 5, Unix, Language
Description 

Author

This package can run parallel processes and manage their execution.

There is a base task class that applications should extend to defined the code that will be executed as a parallel process.

Then there fork manage and task manager classes that start added tasks as processes and manage their execution.

Separated classes can manage the memory and the time that each process can take to execute.

Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 149395 in Germany Germany
Week rank: 49 Up3 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Recommendations

What is the best PHP multithreading class?
How to implement threads in PHP

Example

#!/usr/bin/env php
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-07-20
 */

namespace Net\Bazzline\Component\ProcessForkManager\Example\Basic;

require_once
__DIR__ . '/../bootstrap.php';
require_once
'ExampleTask.php';

use
Net\Bazzline\Component\ProcessForkManager\ForkManagerFactory;

$factory = new ForkManagerFactory();
$manager = $factory->create();
$totalNumberOfTasks = 7;

for (
$iterator = 0; $iterator < $totalNumberOfTasks; ++$iterator) {
   
$task = new ExampleTask();
   
$manager->addTask($task);
}

$manager->getTimeLimitManager()->setBufferInSeconds(2);
$manager->getTimeLimitManager()->setLimitInSeconds(20);

$manager->getMemoryLimitManager()->setBufferInMegaBytes(2);
$manager->getMemoryLimitManager()->setLimitInMegaBytes(64);

$manager->execute();


Details

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: Build Status Latest stable

The scrutinizer status are: code quality | build status

The versioneye status is: dependencies

Downloads: Downloads this Month

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

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 * 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 :-].


  Files folder image Files (33)  
File Role Description
Files folder imageexample (1 file, 5 directories)
Files folder imagesource (9 files)
Files folder imagetest (6 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (33)  /  example  
File Role Description
Files folder imageBasic (2 files)
Files folder imageWithLoggingByUsingEventDispatcher (3 files)
Files folder imageWithMoreTasksThenOpenThreads (2 files)
Files folder imageWithReachingMemoryLimit (2 files)
Files folder imageWithReachingTimeLimit (2 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files (33)  /  example  /  Basic  
File Role Description
  Plain text file ExampleTask.php Class Class source
  Accessible without login Plain text file run Example Example script

  Files folder image Files (33)  /  example  /  WithLoggingByUsingEventDispatcher  
File Role Description
  Plain text file ExampleTask.php Class Class source
  Plain text file LoggerEventSubscriber.php Class Class source
  Accessible without login Plain text file run Example Example script

  Files folder image Files (33)  /  example  /  WithMoreTasksThenOpenThreads  
File Role Description
  Plain text file ExampleTask.php Class Class source
  Accessible without login Plain text file run Example Example script

  Files folder image Files (33)  /  example  /  WithReachingMemoryLimit  
File Role Description
  Plain text file ExampleTask.php Class Class source
  Accessible without login Plain text file run Example Example script

  Files folder image Files (33)  /  example  /  WithReachingTimeLimit  
File Role Description
  Plain text file ExampleTask.php Class Class source
  Accessible without login Plain text file run Example Example script

  Files folder image Files (33)  /  source  
File Role Description
  Plain text file AbstractTask.php Class Class source
  Plain text file ExecutableInterface.php Class Class source
  Plain text file ForkManager.php Class Class source
  Plain text file ForkManagerEvent.php Class Class source
  Plain text file ForkManagerFactory.php Class Class source
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file RuntimeException.php Class Class source
  Plain text file TaskInterface.php Class Class source
  Plain text file TaskManager.php Class Class source

  Files folder image Files (33)  /  test  
File Role Description
  Accessible without login Plain text file AbstractTaskTest.php Test Unit test script
  Accessible without login Plain text file bootstrap.php Test Unit test script
  Accessible without login Plain text file ForkManagerEventTest.php Test Unit test script
  Accessible without login Plain text file ForkManagerTest.php Test Unit test script
  Accessible without login Plain text file ForkManagerTestCase.php Test Unit test script
  Accessible without login Plain text file TaskManagerTest.php Test Unit test script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:217
This week:0
All time:8,296
This week:63Up