PHP Classes

File: example/WithReachingMemoryLimit/run

Recommend this page to a friend!
  Classes of nvb   Process Fork Manager for PHP   example/WithReachingMemoryLimit/run   Download  
File: example/WithReachingMemoryLimit/run
Role: Example script
Content type: text/plain
Description: Example script
Class: Process Fork Manager for PHP
Run parallel processes and manage their execution
Author: By
Last change:
Date: 6 years ago
Size: 1,249 bytes
 

Contents

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

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

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

use
Net\Bazzline\Component\ProcessForkManager\ForkManagerFactory;

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

$taskOne = new ExampleTask();
$taskTwo = new ExampleTask();
$taskThree = new ExampleTask();

$taskOne->setNumberOfDataMultiplication(1);
$taskTwo->setNumberOfDataMultiplication(2);
$taskThree->setNumberOfDataMultiplication(3);

/**
 * @var array|ExampleTask[] $tasks
 */
$tasks = array(
   
1 => $taskOne,
   
2 => $taskTwo,
   
3 => $taskThree
);

foreach (
$tasks as $task) {
   
$manager->addTask($task);
}

$manager->getMemoryLimitManager()->setBufferInMegaBytes(1);
$manager->getMemoryLimitManager()->setLimitInMegaBytes(12);
$manager->execute();

foreach (
$tasks as $key => $task) {
   
$status = ($task->isAborted()) ?
       
'aborted' : (($task->isFinished()) ?
           
'finished' : 'not finished');
    echo
'task ' . $key . ' with run time of ' . $task->getNumberOfDataMultiplication() . ' seconds has been ' . $status . PHP_EOL;
}