Skip to content

Latest commit

 

History

History
124 lines (92 loc) · 4.05 KB

README.md

File metadata and controls

124 lines (92 loc) · 4.05 KB

Concurrent handling of PHP processes (and also closures)

Build Status

The main job of Spawn is to improve the performance handling concurrent processes.

This library tries to do its best to speedup things using all the cores of the machine useful when you need to run bunch of commands eg: unit-tests/functional-tests/CS fixes/files handling

$spawn = new Spawn();
$spawn
    ->processes(range(1,10), "printenv > '/tmp/envs_{}{p}.log';")
    ->onCompleted(function(Process $singleProcess){ /* print stats */});
    ->start();

Concurrent \Closure? Really?

Yes! With this library you can use concurrent closures, BUT PHP is not Go-lang neither Erlang or any other famous language for concurrency, and in order to simulate a isolated routine the closure is serialized and executed in a new PhpProcess, be aware this is a workaround in order to speed up your concurrent Closures.

With this library you can also do:

  1. executes and handles concurrent PHP closures.
  2. spawns a single closure as an independent process.

Concurrent closures: Upload images to your CDN

Feed an iterator and it will break the job into multiple php scripts and spread them across many processes. In order to improve performances, the number of processes is equal to the number of computer's cores.

$spawn = new Spawn();

$files = new RecursiveDirectoryIterator('/path/to/images');
$files = new RecursiveIteratorIterator($files);

$spawn->closures($files, function(SplFileInfo $file) {
    // upload this file
})
->start();

Each closure is executed in isolation using the PhpProcess component.

Spawn a single isolated closure

$spawn = new Spawn();
$sum = 3;

$processes = $spawn
    ->spawn(["super", 120], function($prefix, $number) use ($sum) {
        echo $prefix." heavy routine";
        return $number+$sum;
    });
    
// do something else here
echo $processes->wait();      // 123
echo $processes->getOutput(); // "super heavy routine"

Advanced

  1. The callable is executed in a new isolated processes also with its "use" references.
  2. It's possible to add a listener for event handling.
  3. It's possible to get the return value of each callable, the ErrorOutput, the Output and other information.
$collaborator = new YourCollaborator(1,2,3,4);

$spawn
    ->closures(range(1, 7), function($input) use ($collaborator) {
        echo "this is the echo";
        $collaborator->doSomething();
        $return = new \stdClass();
        $return->name = "name";

        return $return;
    })
    ->onCompleted(function(ClosureProcess $process){
        // do something with
        $returnValue = $processes->getReturnValue();
        $output      = $processes->getOutput();
        $errorOutput = $processes->getErrorOutput();
        $time        = $processes->startAt();
        $memory      = $processes->getMemory();
        $duration    = $processes->getDuration();
    })
    ->start();

Events:

Listeners can be attached to closures and processes.

    ->onStarted(function(ClosureProcess|Process $process){});
    ->onCompleted(function(ClosureProcess|Process $process){});
    ->onSuccessful(function(ClosureProcess|Process $process){});
    ->onEmptyIterator(function (){});
    ->onPartialOutput(function(ClosureProcess|Process $process){})
    ->onLoopCompleted(function ($exitCode, StopwatchEvent $event)

Other libs: