2017 © Pedro Peláez
 

library fork

fork library

image

pbergman/fork

fork library

  • Thursday, May 28, 2015
  • by pbergman
  • Repository
  • 1 Watchers
  • 1 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

processes-fork

a helper class to control, debug and manage process forks. It uses semaphore/message queue from System V as IPC, (*1)

Usage

Simple example:, (*2)

<?php

use PBergman\Fork\Container;
use PBergman\Fork\Work\AbstractWork;
use PBergman\Fork\ForkManager;
use PBergman\Fork\Output;

class job extends AbstractWork
{
    /**
     * add timeout for fun that will generate E_USER_ERROR if time is exceeded
     */
    public function __construct()
    {
        $this->setTimeout(2);
    }

    /**
     * the main method that is called
     *
     * @param  Container $container
     * @return mixed
     */
    public function execute(Container $container)
    {
        $sleep = rand(2, 10);
        $container['output']->debug(sprintf("sleeping %s", $sleep), $this->getPid(), OutputHandler::PROCESS_CHILD);
        sleep($sleep);
    }

    /**
     * a name identifier for logs
     *
     * @return mixed
     */
    public function getName()
    {
        return $this->getPid();
    }
}

$stack = array();

for($i =0 ; $i < 10; $i++){
    $stack[] = new Job();
}

$fm = new ForkManager();
$fm->setWorkers(5)      // add 5 process forks
    ->setJobs($stack)   // add work
    ->run();            // start process

This will give a output like :, (*3)

2014-07-15 12:22:32 [CHILD  ] [28667 ] Starting:
2014-07-15 12:22:32 [CHILD  ] [28668 ] Starting:
2014-07-15 12:22:32 [CHILD  ] [28667 ] sleeping 3
2014-07-15 12:22:32 [CHILD  ] [28669 ] Starting:
2014-07-15 12:22:32 [CHILD  ] [28668 ] sleeping 3
2014-07-15 12:22:32 [CHILD  ] [28669 ] sleeping 4
2014-07-15 12:22:32 [CHILD  ] [28670 ] Starting:
2014-07-15 12:22:32 [CHILD  ] [28670 ] sleeping 5
2014-07-15 12:22:32 [CHILD  ] [28671 ] Starting:
2014-07-15 12:22:32 [CHILD  ] [28671 ] sleeping 9
2014-07-15 12:22:34 [ERROR  ] [28667 ] E_USER_ERROR: timeout exceeded: 2 second(s) on line 147 in file /var/www/processes-fork/src/PBergman/Fork/Work/Controller.php
2014-07-15 12:22:34 [ERROR  ] [28667 ] #0 /var/www/processes-fork/src/PBergman/Fork/Helpers/ErrorHelper.php(62): PBergman\Fork\Helpers\ErrorHelper->(printBackTrace)
2014-07-15 12:22:34 [ERROR  ] [28667 ] #1 (): PBergman\Fork\Helpers\ErrorHelper->(PBergman\Fork\Helpers\{closure})
2014-07-15 12:22:34 [ERROR  ] [28667 ] #2 /var/www/processes-fork/src/PBergman/Fork/Work/Controller.php(147): (trigger_error)
2014-07-15 12:22:34 [ERROR  ] [28667 ] #3 /var/www/processes-fork/src/PBergman/Fork/Work/Controller.php(67): PBergman\Fork\Work\Controller->(PBergman\Fork\Work\{closure})
2014-07-15 12:22:34 [ERROR  ] [28667 ] #4 /var/www/processes-fork/src/PBergman/Fork/ForkManager.php(76): PBergman\Fork\Work\Controller->(run)
2014-07-15 12:22:34 [ERROR  ] [28667 ] #5 /var/www/processes-fork/test.php(56): PBergman\Fork\ForkManager->(run)
2014-07-15 12:22:34 [CHILD  ] [28667 ] Finished: 28667 (0.47 MB/2.86 s)

Methods:

(PBergman\Fork)ForkManager:

construct($debug = false, OutputHelper $output = null, $file = __FILE )

when calling a new instance of the class you can add a instance of output helper so you can specify a output for example:, (*4)

$debug = new OutputHandler();
$debug->setStream(fopen('/tmp/output.log', 'a+'));
$forkmanager = new ForkManager($debug);

this will print log in /tmp/output.log instead of screen, (*5)

The argument $file can be set if jou want to cal this multiple time and don`t want interfere with each other, this has to be existing file. And is used to generate a token with ftok(), (*6)

if debug argument is set to true it will print stack trace for error and warnings, (*7)

getJobs()

Will return the finished jobs that were added by setJobs/addJob (as SplObjectStorage), (*8)

addJob(AbstractWork $job)

will add job to queue, class has to extending AbstractWork, (*9)

setJobs(array $jobs)

will reset job stack and set this given array as jobs (each job have extending AbstractWork), (*10)

setWorkers()

will set set amount of workers that will be spawned (default 1), (*11)

setWorkers()

will set set amount of workers that will be spawned (default 1), (*12)

setMaxSize(int $maxSize)

will set the max size used to read the message queue, (*13)

issues

E_USER_ERROR: Failed to send message, Invalid argument(22)

this means that your msgmax, msgmnb is set to low, this can be fixed by setting msgmax, msgmnb for example to 128MB, (*14)

to set permanently:, (*15)

echo "kernel.msgmax=128000000" >>  /etc/sysctl.conf
echo "kernel.msgmnb=128000000" >>  /etc/sysctl.conf

or configure at runtime:, (*16)

sysctl -w kernel.msgmax=128000000
sysctl -w kernel.msgmnb=128000000

ref: + msgmni: The number of IPC message queue resources allowed (by default, 16). + msgmnb: The size of each message (by default, 8,192 bytes). + msgmax: The maximum total size of the messages in a queue (by default, 16,384 byte..., (*17)

E_USER_ERROR: Failed to receive message, Arg list too long(7)

you have to call method setMaxSize from class ForkManager. And can set the value the same as for example the same you had set for msgmnb (128000000), (*18)


$forkmanager = new ForkManager(); $forkmanager->setWorkers(10) ->setMaxSize(128000000) ->setJobs($work) ->run();

Examples

got 2 examples in the examples folder both te same only deference is that one works with 1 worker (lorem_1_worker.php) and the other with 10 workers (lorem_10_worker.php). This is to see illustrate the time deference between 1 or 10 processes, (*19)

The Versions

28/05 2015

2.0.x-dev

2.0.9999999.9999999-dev

fork library

  Sources   Download

MIT

The Requires

 

by Philip Bergman

fork

28/05 2015

2.0.2

2.0.2.0

fork library

  Sources   Download

MIT

The Requires

 

by Philip Bergman

fork

28/05 2015

2.0.1

2.0.1.0

fork library

  Sources   Download

MIT

The Requires

 

by Philip Bergman

fork

27/05 2015

2.0

2.0.0.0

fork library

  Sources   Download

MIT

The Requires

 

by Philip Bergman

fork

27/10 2014

1.0.x-dev

1.0.9999999.9999999-dev

a helper to fork a process

  Sources   Download

MIT

by Avatar philip

27/10 2014

dev-master

9999999-dev

a helper to fork a process

  Sources   Download

MIT

by Avatar philip

27/10 2014

1.1.0

1.1.0.0

a helper to fork a process

  Sources   Download

MIT

by Avatar philip

23/10 2014

1.0.0

1.0.0.0

a helper to fork a process

  Sources   Download

MIT

by Avatar philip