2017 © Pedro Peláez
 

library php-fork-manager

Simple class to fork and manage processes in PHP and allow multitasking or multiprocessing

image

dmkit/php-fork-manager

Simple class to fork and manage processes in PHP and allow multitasking or multiprocessing

  • Friday, December 16, 2016
  • by dmkit
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

php-fork-manager

A simple class to fork processes and handle multitasking tasks., (*1)

NOTE: I would suggest not to use PHP in forking processes or handling such a case. But if you are like me who were required to use PHP for some reason then feel free to use this., (*2)

Installation

composer require dmkit/php-fork-manager

or in your composer.json, (*3)

{
    "require": {
        "dmkit/php-fork-manager" : "dev-master"
    }
}

then run, (*4)

composer update

Usage

Fork processes and execute multi tasks., (*5)

$manager = new \Dmkit\Fork\Manager\Manager;

// Callback assigned as a worker should be a type hint of callable
// for more info about callable: http://php.net/manual/en/language.types.callable.php

// pass an anonymous function
$manager->addWorker( function(\Dmkit\Fork\Worker\Message\Message $msg) {

    echo "Child Process 1 - Started\n";

    for($i=1; $i <=5; $i++) {
        echo "$i\n";
    }

    echo "Child Process 1 - Ended\n\n";
});

// pass an object
class MyTask 
{
    public function run(\Dmkit\Fork\Worker\Message\Message $msg) {
        echo "Child Process 2 - Started\n";

        for($i=1; $i <=5; $i++) {
            echo "$i\n";
        }

        echo "Child Process 2 - Ended\n\n";
    }
}

$mytaks = new MyTask;

$manager->addWorker([$mytaks, 'run']);

// run the workers
$manager->dispatch();

// execute another batch of processes after the first batch is done

$manager2 = new \Dmkit\Fork\Manager\Manager;

$manager2->addWorker( function(\Dmkit\Fork\Worker\Message\Message $msg) {

    echo "Child Process 1 - Started\n";

    for($i=1; $i <=5; $i++) {
        echo "$i\n";
    }

    echo "Child Process 1 - Ended\n\n";
});

$manager2->dispatch();

The callback gets an argument: Instance of \Dmkit\Fork\Worker\Message\Message. It is used to pass a message or data from a child process to the parent process., (*6)

$manager = new \Dmkit\Fork\Manager\Manager;

$manager->addWorker( function(\Dmkit\Fork\Worker\Message\Message $msg) {

    echo "Child Process 1 - Started\n";

    for($i=1; $i <=5; $i++) {
        echo "$i\n";
    }

    echo "Child Process 1 - Ended\n\n";

  // pass this message to parent process
  // you can pass any data type as the message
    $msg->set('Child Process 1 - Success');
});

// execute each callback in a forked process
$manager->dispatch();

// parse the message passed by the callbacks
// parse the messages passed in the workers
// it can be used to parse error messages or data output from child processes

$messages = $manager->getMessages();


// to get the passed data
foreach($messages as $msg) {
    echo "\n";
    print_r($msg->get());
}

This plugin requires the PCNTL extension (http://php.net/manual/en/function.pcntl-fork.php )., (*7)

Messages passed by the child processes are saved to memory using SHMOP extension ( https://packagist.org/packages/anime-db/shmop ) and anime-db/shmop (https://packagist.org/packages/anime-db/shmop) as the wrapper class., (*8)

The Versions

16/12 2016

dev-master

9999999-dev

Simple class to fork and manage processes in PHP and allow multitasking or multiprocessing

  Sources   Download

The Requires

 

by Dan Bangayan