PHP code example of sj-i / ffi-zts-parallel

1. Go to this page and download the library: Download sj-i/ffi-zts-parallel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sj-i / ffi-zts-parallel example snippets



SjI\FfiZts\Parallel\Parallel;

Parallel::boot()
    ->runScript(__DIR__ . '/worker.php');


$futures = [];
for ($i = 0; $i < 4; $i++) {
    $rt        = new parallel\Runtime();
    $futures[] = $rt->run(function (int $id): array {
        $sum = 0;
        for ($j = 0; $j < 2_000_000; $j++) $sum += $j;
        return ['id' => $id, 'pid' => getmypid(), 'zts' => PHP_ZTS, 'sum' => $sum];
    }, [$i]);
}
foreach ($futures as $f) {
    print_r($f->value());
}

Parallel::boot()
    ->withIniEntry('opcache.enable_cli',  '1')
    ->withIniEntry('opcache.preload',     __DIR__ . '/preload.php')
    ->withIniEntry('opcache.preload_user', get_current_user())
    ->runScript(__DIR__ . '/worker.php');

Parallel::bootInMemory()->runScript('worker.php');