2017 © Pedro Peláez
 

library forker

PHP posix process manager and async job handler

image

edwardstock/forker

PHP posix process manager and async job handler

  • Monday, December 19, 2016
  • by edwardstock
  • Repository
  • 1 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

PHP Forker

PHP POSIX process manager and async ProcessPool, (*1)

Build Status, (*2)

News

  • 1.0.2, (*3)

    • process pooling fix and added method to run process with limited count per "now":
    <?php
    $pm = new ProcessManager();
    $pm->pool(2 /*pool size*/, true /*join processes*/);
    
    for($i = 0; $i < 10; $i++) {
    // when 2 jobs will added to pm queue, they will run, and cleaned after complete
    // calling $pm->run() manually not needed
    $pm->add($job); 
    }
    
  • 1.0.1, (*4)

    • Added support for custom arguments in background funciton

Features

  • Easy to create multi-processed daemons
  • POSIX Signals dispatching
  • Serializing objects/arrays that contains closures (thx to SuperClosure)
  • Uses shared memory
  • *.pid file managing

Usage examples

Basic usage

<?php
use edwardstock\forker\handler\CallbackTask;
use edwardstock\forker\ProcessManager;

$updated = 0;

// simple background job
$bigTableUpdate = CallbackTask::create(function(CallbackTask $task) {

    return DB::bigQuery('UPDATE very_big_table SET field=TRUE WHERE another=FALSE'); //it's just example

})->future(function($updatedCount, CallbackTask $task) use(&$updated) {

    $updated = $updatedCount;

})->error(function(\Throwable $exception, CallbackTask $task){

    // handle exception occurred while DB::bigQuery()
    Logger::log($exception);

});

$processManager = new ProcessManager('/path/to/file.pid');
$processManager
    ->add($bigTableUpdate)
    ->run(true) // true - join to main process, if you don't have an expensive and complex logic in future method
    ->wait(); // wait while process will complete doing job
    // if don't call wait() method, process will be detached from terminal or main process and continue to working in background

echo $updated; // count of updated very_big_table

That was just a very simple example, now more useful, (*5)

Batch usage

<?php
use edwardstock\forker\handler\CallbackTask;
use edwardstock\forker\handler\BatchTask;
use edwardstock\forker\ProcessManager;
$toDownload = [
    'https://google.com',
    'https://facebook.com',
];

$results = [];


/** @var BatchTask $downloads */
$downloads = BatchTask::create($toDownload, function ($toDownloadItem, CallbackTask $task) {

    return @file_get_contents($toDownloadItem);

})->future(function ($sitesContent, BatchTask $task) use (&$results) {

    $results = $sitesContent;

});

$pm = new ProcessManager();
$pm->add($downloads);
$pm->run(true)->wait(); 

var_dump($results); 
// result
// array(2) {
//     0 => string(28) 'html_content_from_google.com'
//     1 => string(30) 'html_content_from_facebook.com'
// }

// Order of results in this case is random, cause, for example,
// facebook.com can be downloaded faster than google.com

More examples will soon... ;), (*6)

The Versions

19/12 2016

dev-master

9999999-dev https://github.com/edwardstock/forker

PHP posix process manager and async job handler

  Sources   Download

GNU General Public License v3.0

The Requires

 

The Development Requires

process pcntl fork posix multi-thread multi-process multi-threaded php-async

19/12 2016

1.0.2

1.0.2.0 https://github.com/edwardstock/forker

PHP posix process manager and async job handler

  Sources   Download

GNU General Public License v3.0

The Requires

 

The Development Requires

process pcntl fork posix multi-thread multi-process multi-threaded php-async

15/12 2016

1.0.1

1.0.1.0 https://github.com/edwardstock/forker

PHP posix process manager and async job handler

  Sources   Download

GNU General Public License v3.0

The Requires

 

The Development Requires

process pcntl fork posix multi-thread multi-process multi-threaded php-async

09/12 2016

1.0.0

1.0.0.0 https://github.com/edwardstock/forker

PHP posix process manager and async job handler

  Sources   Download

GNU General Public License v3.0

The Requires

 

The Development Requires

process pcntl fork posix multi-thread multi-process multi-threaded php-async