PHP code example of toalett / multiprocessing

1. Go to this page and download the library: Download toalett/multiprocessing 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/ */

    

toalett / multiprocessing example snippets


use Toalett\Multiprocessing\ContextBuilder;

$builder = ContextBuilder::create();
$context = $builder->build();

use Toalett\Multiprocessing\ContextBuilder;

$context = ContextBuilder::create()->build();

$job = static function(string $name) {
    print("Hello from {$name}!\n");
    usleep(500_000);
    print("Goodbye from ${name}!\n");
};

$context->submit($job, 'John Snow');

use Toalett\Multiprocessing\Concurrency;
use Toalett\Multiprocessing\ContextBuilder;

$context = ContextBuilder::create()
    ->withConcurrency(Concurrency::atMost(2))
    ->build();

// $job = function(...)...

foreach(['John', 'Stannis', 'Jorah', 'Robert', 'Daario'] as $name) {
    $context->submit($job, $name);
}

$context->run();

use React\EventLoop\Factory;
use Toalett\Multiprocessing\ContextBuilder;

$loop = Factory::create();
$context = ContextBuilder::create()
    ->withEventLoop($loop)
    ->build();

// Submit a job every 5 seconds
$loop->addPeriodicTimer(5.0, fn() => $context->submit(...));
$context->run();

$context->on('name_of_event', fn() => ...);

use Toalett\Multiprocessing\ContextBuilder;
use Toalett\Multiprocessing\Task\Interval;

const NUM_JOBS = 50;

$context = ContextBuilder::create()
    ->withCleanupInterval(Interval::seconds(0.5))
    ->build();

$counter = new Counter();
$context->on('worker_stopped', [$counter, 'increment']);
$context->on('no_workers_remaining', [$context, 'stop']);
$context->on('stopped', fn() => printf(" %d\n", $counter->value));

for ($i = 0; $i < NUM_JOBS; $i++) {
    $context->submit(fn() => sleep(2));
    print('.');
}

$context->run();

use React\EventLoop\Factory;
use Toalett\Multiprocessing\ContextBuilder;
use Toalett\Multiprocessing\Concurrency;

$loop = Factory::create();
$context = ContextBuilder::create()
    ->withEventLoop($loop)
    ->withConcurrency(Concurrency::atMost(4))
    ->build();

$context->on('booted', fn() => print("🚽 toalett context booted\n"));
$context->on('congestion', fn() => print('C'));
$context->on('congestion_relieved', fn() => print('R'));
$context->on('worker_started', fn() => print('+'));
$context->on('worker_stopped', fn() => print('-'));

// A job is submitted to the context every second.
// The job sleeps for a random amount of seconds (0 - 10).
$loop->addPeriodicTimer(1, fn() => $context->submit(fn(int $s) => sleep($s), random_int(0, 10)));

print("Press CTRL+C to stop.\n");
$context->run();

use Toalett\Multiprocessing\Concurrency;
use Toalett\Multiprocessing\ContextBuilder;
use Toalett\Multiprocessing\Task\Interval;

$context = ContextBuilder::create()
    ->withConcurrency(Concurrency::singleWorker())
    ->withCleanupInterval(Interval::seconds(0.2))
    ->build();

for ($i = 0; $i < 3; $i++) {
    $title = md5(mt_rand());
    $context->submit(new Job($title));
}

$context->on('no_workers_remaining', [$context, 'stop']);
$context->run();